#
# Makefiles header
# OS   : Linux, QNX, Solaris, HPUX
# Tool : gcc or native complier
#
# This header is included into the individual
# makefiles for the runtime and samples
#
# Copyright (c) 2001-2012 McObject LLC.
# 
#
#

## Build control switches ---------------------------------
# Remove the comment from follows switches or type:
# make x64=on               - to build x64 version
# make SHOW_BUILD_LOG=on    - to see building progress
# make DEBUG=on             - to make a debug version of the runtime
# make OPTIMIZE=size        - to minimize the runtime size
# make OPTIMIZE=speed       - to maximize the runtime perfomance
# make STATIC=on            - to enable static linkage of executables
# make HOST_PLATFORM=       - to build tools and utilites for specified host platform
# make HOST_FLAGS=          - to specify additional flags for host platform
# make TARGET_PLATFORM=     - to build runtime and samples for specified target platform (mipsel,arm,etc)
# make TARGET_FLAGS=        - to specify additional flags for target platform (ex -mcpu=XXXX)
# make TARGET_OS_HINT=      - to tune up the runtime for specified target OS (ex. uCLinux or LynxOS)
# make MCO_LIB_DYNAMIC=     - to build dynamic version (.so) of the runtime
# make MCO_LIB_STATIC=      - to build static version (.a) of the runtime
# make TARGET_ARCH=         - to override target's and/or
# make HOST_ARCH=           - host's cpu architecture
#                             possible values: arm|mips|ix86|ppc|m68k|sparc64
# make TARGET_CPUNO=        - to set number of cpu on host's or target's. 
# make HOST_CPUNO=          - Default setting is 2 to enforce SMP behaviour.
# make SAMPLE_STRG=         - set database storage for a sample (memory or disk). Default is memory 
#                             Note: applicable to samples only. Ignored in other cases.
# make MCO_FS=              - to build samples for specified file system. Default depends on TARGET_PLATFORM 
# make MCO_USE_ARCH_SFX=    - to enable (on) or disable (off) name suffixing of object and binary directories. 
#                             This option is useful when you need to build binaries for different architectures
#                             in the same source tree. Disabled by default.
# make MCO_LARGE_DATABASE_SUPPORT=on - to enable support databases larget then 4GB on 32-bit targets. Defailt disabled 
#                             for 32-bit and enabled for 64-bit cases.
# make JAVA=                - on or off, enables JNI support and java samples

#SHOW_BUILD_LOG=on
#DEBUG=on
#OPTIMIZE=speed
#OPTIMIZE=size
#STATIC=on
#HOST_PLATFORM=
#HOST_FLAGS=-g
#TARGET_PLATFORM=mipsel-linux|arm-linux
TARGET_PLATFORM=tile
#TARGET_OS_HINT=uCLinux
#TARGET_FLAGS=-mips2|-m5307
#TARGET_FLAGS=-g
#MCO_LIB_DYNAMIC=enabled
#MCO_LIB_DYNAMIC=disabled
#MCO_LIB_STATIC=enabled
#MCO_LIB_STATIC=disabled
#TARGET_ARCH=arm|mips|ix86|ppc|m68k|sparc64
#TARGET_ARCH=ix86
#HOST_ARCH=arm|mips|ix86|ppc|m68k|sparc64
#HOST_ARCH=ix86
HOST_CPUNO  ?= 2
TARGET_CPUNO?= 2
#SAMPLE_STRG=memory|disk
#MCO_FS=ucos|w32|funi|fu98|<custom>
#INSTALL_DIR=
#MCO_USE_ARCH_SFX=on
#MCO_USE_ARCH_SFX=off
#MCO_LARGE_DATABASE_SUPPORT:=on
MCO_LARGE_DATABASE_SUPPORT?=off
JAVA:=off

## Platforms ----------------------------------------------
BUILD_PLATFORM   ?= $(shell uname -s)
BUILD_ARCH       ?= $(shell uname -m | sed -e's/i.86/ix86/' -e's/^[xX]86.*_64/ix86_64/' -e's/^[xX]86.*/ix86/' -e's/sun4u/sparc64/' -e's/arm.*/arm/' -e's/sa110/arm/' -e's/mips.*/mips/' -e's/ppc.*/ppc/' -e's/powerpc.*/ppc/' -e's/m68k.*/m68k/')

AUTODETECTED_PLATFORM := $(BUILD_PLATFORM)

ifeq ($(AUTODETECTED_PLATFORM),Darwin)
BUILD_PLATFORM   := Linux
BUILD_ARCH := ix86_64
TEST_FILE = false
else
TEST_FILE = test -f
endif

ifeq ($(findstring CYGWIN_NT, $(AUTODETECTED_PLATFORM)),CYGWIN_NT)
TEST_FILE = false
endif

HOST_PLATFORM    ?= $(BUILD_PLATFORM)
TARGET_PLATFORM  ?= $(BUILD_PLATFORM)
ENV_CC           := $(CC)
ENV_CXX          := $(CXX)

ifeq ($(BUILD_ARCH),ix86_64)
ifneq ($(x64),on)
BUILD_ARCH       := ix86
endif
endif

include $(MCO_ROOT)/include/package-spec.mak
ifneq ($(strip $(MCO_PACKAGE_DEFINED)),yes)
$(error Package was not defined) 
endif

## Common build variables ---------------------------------------------

ifneq ($(PRJ_TYPE),APP)
SAMPLE           ?= no                           # must be defined in sample makefile 
SAMPLE_STRG      ?= memory                       # default sample storage mode "in-memory"
SAMPLE_INCLUDE    = -I$(MCO_ROOT)/platform
MEMORY_SFX       := 
DISK_SFX         := -d
else
MCO_SIDE=$(PRJ_SIDE)
endif # PRJ_TYPE!=APP

ifeq ($(MCO_USE_ARCH_SFX),on)
BUILD_ARCH_SFX    = -$(BUILD_ARCH)
MCO_ARCH_SFX      = -$($(MCO_SIDE)_ARCH)
else
BUILD_ARCH_SFX   :=
MCO_ARCH_SFX     := 
endif

MCO_INCLUDE       = -I$(MCO_ROOT)/include  -I$(MCO_ROOT)/include/ha
MCO_SQL_INCLUDE   = -I$(MCO_ROOT)/include/sql
MCO_ODBC_INCLUDE  = -I$(MCO_ROOT)/include/sql/odbc

# by default all binaries comes to bin
MCO_BIN_DIR_BASE := bin

# if DPTR build is on and it is not host side binary and there are no offset-binaries in the package put binaries to bin
ifeq "$(MCO_DIRECT_PTR)" "on"
 ifneq "$(MCO_SIDE)" "HOST"
   ifeq "$(strip $(MCO_PACKAGE_REGL))" "yes"
    MCO_BIN_DIR_BASE := bin.dptr
   endif
 endif
endif

MCO_TARGET        = $(MCO_ROOT)/target
MCO_HOST          = $(MCO_ROOT)/host
MCO_ODBC          = $(MCO_ROOT)/odbc
MCO_BIN           = $(MCO_$(MCO_SIDE))/$(MCO_BIN_DIR_BASE)$(MCO_ARCH_SFX)
MCO_BIN_SO        = $(MCO_$(MCO_SIDE))/$(MCO_BIN_DIR_BASE)$(MCO_ARCH_SFX).so
MCO_ODBC_BIN      = $(MCO_ODBC)/$(MCO_BIN_DIR_BASE)$(MCO_ARCH_SFX)
MCO_ODBC_BIN_SO   = $(MCO_ODBC)/$(MCO_BIN_DIR_BASE)$(MCO_ARCH_SFX).so
MCO_LIB_SRC       = $(MCO_TARGET)/mcolib
MCO_BUILD_LOG_DIR = $(MCO_ROOT)/_build-logs$(MCO_ARCH_SFX)
ifndef GENERATOR
MCOCOMP           = $(MCO_HOST)/bin$(BUILD_ARCH_SFX)/mcocomp
MCORCOMP          = $(MCO_HOST)/bin$(BUILD_ARCH_SFX)/mcorcomp
GEN_INMEM         =
else
MCOCOMP           = echo -e "\nMCOCOMP: "
MCORCOMP          = echo -e "\nMCORCOMP: "
GEN_INMEM         =

ifeq ("$(MCO_VX_INMEM)","on")
GEN_INMEM         =.inmem
endif

endif

MCOCOMP_EXTRA_FLAGS :=

LIBSWITCH         = -L
LINKSWITCH        = -l
LIBSUFFIX         =
OBJSUFFIX         = .o
EXESUFFIX         = 

MCO_CFG_SUFFIX_A  = .a
MCO_CFG_SUFFIX_SO = .so
MCO_LOGFILE_SFX  := .log

CHECK_SUFFIX     := debug
EVAL_SUFFIX      := eval

MCO_CFG_OVERRIDE := # clear initialy 
ifeq ($(BUILD_PLATFORM), WIN32)
CHECKFLAGS       := /D "MCO_CONFIG_OVERRIDE_CHECKLEVEL" /D "MCO_CFG_CHECKLEVEL_1" /D "MCO_CFG_CHECKLEVEL_2" /D "MCO_CFG_CHECKLEVEL_3"
else
CHECKFLAGS       := -DMCO_CONFIG_OVERRIDE_CHECKLEVEL -DMCO_CFG_CHECKLEVEL_1 -DMCO_CFG_CHECKLEVEL_2 -DMCO_CFG_CHECKLEVEL_3
endif
RELEASEFLAGS     := # just as specified in mcocfg.h configuration file

MCO_VERSION       = test -f $(MCO_ROOT)/include/version && cat $(MCO_ROOT)/include/version
MCO_SQLLIB_EXISTS = test -f $(MCO_BIN)/libmcolib_sql.a -o -f $(MCO_BIN_SO)/libmcolib_sql.so && echo present

MCO_RTODBC        = -L$(MCO_ODBC)/bin $(LINKSWITCH)mcoodbc$(MCO_RTCHECK)
MCO_RTSQL         = $(LINKSWITCH)mcosql$(MCO_RTCHECK) $(LINKSWITCH)mcoapi$(MCO_RTCHECK)
MCO_RTRSQL        = $(LINKSWITCH)mcorsql$(MCO_RTCHECK)
MCO_RTLIB         = 
MCO_RPCLIB        = $(LINKSWITCH)mcorpc$(MCO_RTCHECK)

ifdef DEBUG
MCO_RTCHECK      := _$(CHECK_SUFFIX)
else
MCO_RTCHECK      :=
endif

ifeq "$(MCO_DIRECT_PTR)" "on"
MCO_OBJ_DIR_BASE := obj-dptr
else
MCO_OBJ_DIR_BASE := obj
endif

ifdef MCO_OBJ_DIR
MCO_OBJ_DIR      := $(addprefix $(MCO_OBJ_DIR_BASE),$(MCO_OBJ_DIR))
else
MCO_OBJ_DIR      := $(MCO_OBJ_DIR_BASE)
endif

#  Default FS setting
ifeq ($(TARGET_OS_HINT),LynxOS)
DEFAULT_FS_LINUX          = uni
else
DEFAULT_FS_LINUX          = u98
endif
DEFAULT_FS_WIN32          = w32
DEFAULT_FS_SOLARIS        = u98
DEFAULT_FS_QNX            = u98
DEFAULT_FS_HPUX           = u98
DEFAULT_FS_ADS            = ucos
DEFAULT_FS_OS21           = u98
DEFAULT_FS_VXWORKS        = vx
DEFAULT_FS_ECOS           = ecos
DEFAULT_FS_NETOS          = 
MCO_FS                   ?= $(DEFAULT_FS$(subst -D,,$(PLATFORM_ID)))
MCO_FS_LIB                = $(LINKSWITCH)mcof$(strip $(MCO_FS))

#  Default sync. primitives for ...
#     ... shared memory mode
ifeq ($(TARGET_OS_HINT),LynxOS)
DEFAULT_SYNC_S_LINUX     := lynx
else
DEFAULT_SYNC_S_LINUX     := lnx   # possible alternative is lnxs5 
endif
DEFAULT_SYNC_S_WIN32     := w32
DEFAULT_SYNC_S_SOLARIS   := sun
DEFAULT_SYNC_S_QNX       := qnxp
DEFAULT_SYNC_S_HPUX      := hpuxp
DEFAULT_SYNC_S_ADS       := ucsn # possible alternatives are ucos ucsn
DEFAULT_SYNC_S_ECOS      := ecos
DEFAULT_SYNC_S_OS21      := os21
DEFAULT_SYNC_S_VXWORKS   := vxshm
DEFAULT_SYNC_S_NETOS     := ntos
#    ... private memory mode
ifeq ($(TARGET_OS_HINT),LynxOS)
DEFAULT_SYNC_C_LINUX     := lynxp
else
DEFAULT_SYNC_C_LINUX     := lnxp   # possible alternative is lnxsp 
endif
DEFAULT_SYNC_C_WIN32     := w32
DEFAULT_SYNC_C_SOLARIS   := sun
DEFAULT_SYNC_C_QNX       := qnxp
DEFAULT_SYNC_C_HPUX      := hpuxp
DEFAULT_SYNC_C_ADS       := ucsn # possible alternatives are ucos ucsn
DEFAULT_SYNC_C_ECOS      := ecos
DEFAULT_SYNC_C_OS21      := os21
DEFAULT_SYNC_C_VXWORKS   := vxn
DEFAULT_SYNC_C_NETOS     := ntos

# Default memory driver for ...
#    ... shared memory mode
DEFAULT_MEM_S_LINUX        := ipc
DEFAULT_MEM_S_WIN32        := w32
DEFAULT_MEM_S_SOLARIS      := ipc
DEFAULT_MEM_S_QNX          := psx
DEFAULT_MEM_S_HPUX         := ipc
DEFAULT_MEM_S_ADS          := conv # there is no shared memory
DEFAULT_MEM_S_ECOS         := conv # there is no shared memory
DEFAULT_MEM_S_OS21         := conv # there is no shared memory
DEFAULT_MEM_S_VXWORKS      := psx
DEFAULT_MEM_S_NETOS        := psx
#    ... private memory mode
DEFAULT_MEM_C_LINUX        := conv
DEFAULT_MEM_C_WIN32        := conv 
DEFAULT_MEM_C_SOLARIS      := conv 
DEFAULT_MEM_C_QNX          := conv 
DEFAULT_MEM_C_HPUX         := conv 
DEFAULT_MEM_C_ADS          := conv 
DEFAULT_MEM_C_ECOS         := conv 
DEFAULT_MEM_C_OS21         := conv
DEFAULT_MEM_C_VXWORKS      := conv
DEFAULT_MEM_C_NETOS        := conv
#  Default RTL settings
MCO_MISC                 ?= wrt # Use runtime library. Default. 
MCO_MISC                 ?= nrt # Do not use runtime library.
MCO_MISC_LIB              = $(LINKSWITCH)mcou$(strip $(MCO_MISC))
MCO_RTLIB                += $(MCO_MISC_LIB)$(MCO_RTCHECK)

#  Default transaction manager
MCO_TMGR                 ?= mursiw # possible values are :
                                   # "exlc" (exclusive) or 
                                   # "mursiw" (multiple readers single writer) or 
                                   # "mvcc" (multiverioned concurrent control)
MCO_TMGR_LIB              = $(LINKSWITCH)mcot$(strip $(MCO_TMGR))

ifeq "$(MCO_DIRECT_PTR)" "on"
ifeq ($(BUILD_PLATFORM), WIN32)
MCO_CFG_OVERRIDE += /D "MCO_CFG_USE_DIRECT_POINTERS"
else
MCO_CFG_OVERRIDE += -DMCO_CFG_USE_DIRECT_POINTERS
endif
ifeq "$(SAMPLE_STRG)" "disk"
override SAMPLE_STRG      := memory
endif
endif 

ifneq ($(PRJ_TYPE),APP)
ifeq "$(SAMPLE)" "yes"
MCO_INCLUDE      += $(SAMPLE_INCLUDE) -I$(MCO_OBJ_DIR)
MCOCOMP          += -P$(MCO_OBJ_DIR)/
ifeq "$(SAMPLE_SQL)" "yes"
MCO_INCLUDE      += $(MCO_SQL_INCLUDE)
MCOCOMP_SQL_F    := -sql
MCO_RTLIB        += $(MCO_RTSQL)
ifeq "$(SAMPLE_RSQL)" "yes"
MCO_RTLIB        += $(MCO_RTRSQL)
endif
ifeq ($(shell $(MCO_SQLLIB_EXISTS)),present)
MCO_RTCORE       := $(LINKSWITCH)mcolib$(MCO_RT)_sql$(MCO_RTCHECK)
else
MCO_RTCORE       := $(LINKSWITCH)mcolib$($(MCO_RT):_sql=)$(MCO_RTCHECK)
endif
else
MCOCOMP_SQL_F    :=
MCO_RTCORE       := $(LINKSWITCH)mcolib$(MCO_RT)$(MCO_RTCHECK)
endif

MCO_RTLIB        += $(MCO_RTCORE)

ifeq "$(SAMPLE_STRG)" "disk"
MCO_LOGFILE_SFX  := $(addprefix $(DISK_SFX),$(MCO_LOGFILE_SFX))
MCO_OBJ_DIR      := $(addsuffix $(DISK_SFX),$(MCO_OBJ_DIR))
MCO_VT           := dsk
ifeq ($(BUILD_PLATFORM), WIN32)
SAMPLE_FLAGS     := /D "DISK_DATABASE" /D "DB_LOG_TYPE=REDO_LOG"
else
SAMPLE_FLAGS     := -DDISK_DATABASE -DDB_LOG_TYPE=REDO_LOG
endif
MCOCOMP_STRG_F   := -persistent
STRG_SFX         := $(DISK_SFX)
else
MCO_LOGFILE_SFX  := $(addprefix $(MEMORY_SFX),$(MCO_LOGFILE_SFX))
MCO_OBJ_DIR      := $(addsuffix $(MEMORY_SFX),$(MCO_OBJ_DIR))
MCO_VT           := mem
ifeq ($(BUILD_PLATFORM), WIN32)
SAMPLE_FLAGS     := /D "MEMORY_DATABASE"
else
SAMPLE_FLAGS     := -DMEMORY_DATABASE
endif
MCOCOMP_STRG_F   := 
STRG_SFX         := $(MEMORY_SFX)
endif
else
MCO_OBJ_DIR_SFX  :=
endif
endif # PRJ_TYPE!=APP

MCO_RTLIB        += $(LINKSWITCH)mcovt$(MCO_VT)$(MCO_RT)$(MCO_RTCHECK)

ifeq ($(MCO_VT), dsk)
MCO_RTLIB                += $(MCO_FS_LIB)$(MCO_RTCHECK)
endif

ifeq ($(MCO_VT), dsk)
 MCO_RTSQL += $(LINKSWITCH)mcoseridsk$(MCO_RTCHECK)
else
 MCO_RTSQL += $(LINKSWITCH)mcoseri$(MCO_RTCHECK)
endif

ifeq ($(MCOCOMP_SQL_F), -sql)
 ifeq ($(MCO_VT), dsk)
  MCO_RTLIB += $(LINKSWITCH)mcoseridsk$(MCO_RTCHECK)
 else
  MCO_RTLIB += $(LINKSWITCH)mcoseri$(MCO_RTCHECK)
 endif
endif

ifeq ($(TARGET_OS_HINT),LynxOS)
DEFAULT_SYNC_LINUX_SHM   := lynx
DEFAULT_SYNC_LINUX_CNV   := lynxp
else
DEFAULT_SYNC_LINUX_SHM   := lnx   # possible alternative is lnxs5 
DEFAULT_SYNC_LINUX_CNV   := lnxp  # possible alternative is lnxsp 
endif
DEFAULT_SYNC_WIN32       := w32
DEFAULT_SYNC_SOLARIS     := sun
DEFAULT_SYNC_QNX         := qnxp
DEFAULT_SYNC_HPUX        := hpuxp
DEFAULT_SYNC_ADS         := ucsn # possible alternatives are ucos ucsn
DEFAULT_SYNC_ECOS        := ecos
DEFAULT_SYNC_OS21        := os21
MCO_SYNC                 ?= $(DEFAULT_SYNC$(subst -D,,$(PLATFORM_ID)))
MCO_SYNC_LIB              = $(LINKSWITCH)mcos$(strip $(MCO_SYNC))
ifeq ($(SHM_APP),yes)
DEFAULT_SYNC_LINUX        = $(DEFAULT_SYNC_LINUX_SHM)
else
DEFAULT_SYNC_LINUX        = $(DEFAULT_SYNC_LINUX_CNV)
endif
MCO_RTLIB                += $(MCO_SYNC_LIB)$(MCO_RTCHECK)

DEFAULT_SHM_LINUX        := ipc
DEFAULT_SHM_WIN32        := w32
DEFAULT_SHM_SOLARIS      := ipc
DEFAULT_SHM_QNX          := psx
DEFAULT_SHM_HPUX         := ipc
DEFAULT_SHM_ADS          := # none
DEFAULT_SHM_ECOS         := # none
DEFAULT_SHM_OS21         := # none

ifeq ($(SHM_APP),yes)
MCO_MEM                  ?= $(DEFAULT_SHM$(subst -D,,$(PLATFORM_ID)))
else
MCO_MEM                  ?= conv
endif

ifneq ($(strip MCO_MEM),)
MCO_MEM_LIB               = $(LINKSWITCH)mcom$(strip $(MCO_MEM))
MCO_RTLIB                += $(strip $(MCO_MEM_LIB))$(MCO_RTCHECK)
endif

MCO_RTLIB                += $(MCO_TMGR_LIB)$(MCO_RTCHECK) $(MCO_RTCORE)

# rt variants
MCO_OBJ_VSFX     := # Variation suffix 
MCO_BUILDRT_SFX_M:= # no modification suffix initially
MODFLAGS         := # no modification flags initially

ifeq ($(MCO_BUILD_RT_DEBUG),on)
MCO_CFG_OVERRIDE += $(CHECKFLAGS)
MCO_FS_LIB       := $(addsuffix _$(CHECK_SUFFIX), $(MCO_FS_LIB))
MCO_SYNC_LIB     := $(addsuffix _$(CHECK_SUFFIX), $(MCO_SYNC_LIB))
MCO_MEM_LIB      := $(addsuffix _$(CHECK_SUFFIX), $(MCO_MEM_LIB))
DEBUG            :=on
ifeq ($(MCO_BUILDRT_SFX_C),)
MCO_BUILDRT_SFX_C:= _$(CHECK_SUFFIX)
else
MCO_BUILDRT_SFX_C:= $(addsuffix _$(CHECK_SUFFIX), $(MCO_BUILDRT_SFX_C))
endif
MCO_OBJ_DIR      := $(addsuffix -$(CHECK_SUFFIX), $(MCO_OBJ_DIR))
MCO_TITLE        := $(addsuffix -$(CHECK_SUFFIX), $(MCO_TITLE))
else
MCO_CFG_OVERRIDE+= $(RELEASEFLAGS)
endif

ifeq ($(MCO_BUILD_RT_EVAL),on)

MCO_CFG_OVERRIDE += -DMCO_CFG_TRLIMIT=1000000 

MCO_BUILDRT_SFX_E:= _$(EVAL_SUFFIX)
MCO_OBJ_DIR      := $(addsuffix -$(EVAL_SUFFIX), $(MCO_OBJ_DIR))
MCO_TITLE        := $(addsuffix -$(EVAL_SUFFIX), $(MCO_TITLE))
endif

ifeq ($(MCO_BUILD_RT_VTDSK_SPINS),on)
MCO_OBJ_VSFX     := _spn
MCO_BUILDRT_SFX_M:= _spn
MODFLAGS         := -DMCO_TUNE_SPINLOCKED_DISKPOOL
endif

MCO_BUILDRT_SFX  := $(MCO_BUILDRT_SFX_M)$(MCO_BUILDRT_SFX_C)$(MCO_BUILDRT_SFX_E)

##  Testing arch file ------------------------------------------------------
ifneq ($(SHOW_X64_ARCH),on)

xArchBoth = off
ifeq ($(shell test -f $(MCO_ROOT)/x32_arch_binaries && echo present),present)
x32Arch = on
else
x32Arch = off
endif
ifeq ($(shell test -f $(MCO_ROOT)/x64_arch_binaries && echo present),present)
x64Arch = on
else
ifeq ($(x32Arch),off)
x32Arch = on
x64Arch = on
xArchBoth = on
else
x64Arch = off
endif
endif

ifeq ($(x64),)
ifeq ($(xArchBoth),off)
ifeq ($(x64Arch),off)
x64 = off
else
x64 = on
endif 
endif
else

## Test binary compatibility
ifeq ($(x64),on)

ifeq ($(x64Arch),off)
$(error 64bit app build is not supported.)
endif

else

ifeq ($(x32Arch),off)
$(error 32bit app build is not supported.)
endif

endif
 
endif 
endif


ifeq ($(x64),on)
MCO_LARGE_DATABASE_SUPPORT := on
else
ifeq ($(MCO_DIRECT_PTR),on)
ifeq ($(MCO_LARGE_DATABASE_SUPPORT),on)
$(error Large database is not supported for 32-bit direct pointer configuration)
endif
endif
endif

## end of arch testing -------------------------------------------------

## Platform-dependent variables ----------------------------------------
# Common settings
NMAKE             = nmake.bat
RMFILE            = rm -f
RMDIR             = rm -rf
MKDIR            := mkdir -p
CPFILE            = cp -f
AR                = ar
ARFLAGS           = rc
STRIP             = strip
BISON_           := $(shell which bison > /dev/null 2>&1; if [ 1 -eq 0 ]; then echo bison -d; fi )
YACC_            := $(shell which yacc > /dev/null 2>&1; if [ 1 -eq 0 ]; then echo yacc; fi )
ifeq ($(strip $(BISON_)),)
ifneq ($(strip $(YACC_)),)
BISON            := $(YACC_)
else
BISON            :=
endif
else
BISON            := $(BISON_)
endif

ARCH_FLAGS        =
COMMONFLAGS       = $(DEBUGFLAGS) $(OPTIMIZEFLAGS) $($(MCO_SIDE)_FLAGS) $(SAMPLE_FLAGS) $(MODFLAGS)
CFLAGS            = $(COMMONFLAGS) $(PLATFORM_ID) $(ARCH_FLAGS) -Wall # -pedantic
CXXFLAGS          = $(COMMONFLAGS) $(PLATFORM_ID) $(ARCH_FLAGS) -fexceptions $(CXXTEMPLATEDEPTHF) -Wall
SOCFLAGS          = $(PICF) $(PLATFORM_ID) $(ARCH_FLAGS) 
SOCXXFLAGS        = $(PICF) $(PLATFORM_ID) $(ARCH_FLAGS) 
SOLDFLAGS         = $(COMMONFLAGS) $(PICF) $(SHAREDF)
LDFLAGS           = $(COMMONFLAGS)
LDCXXFLAGS        = $(COMMONFLAGS)
STATIC            = on
ifdef STATIC
LDFLAGS          += $(STATICF)
LDCXXFLAGS       += $(STATICF)
endif
LDFLAGS           += 
LDCXXFLAGS        += -fexceptions

MCO_LIB_DYNAMIC  ?= enabled
MCO_LIB_STATIC   ?= enabled
MCO_SHAREDMEM    ?= enabled

ifndef GENERATOR
CC               ?= gcc
CXX              ?= g++
LD                = $(CC)
LDCXX             = $(CXX)
else
CC = echo -e "\nCOMPILER: "
CXX = echo -e "\nCOMPILER: "
LD = echo -e "\nLINKER: "
LDCXX = echo -e "\nLINKER: "
AR = echo -e "\nLIBRARIAN: "
#ARFLAGS = 
#LDFLAGS =
MSG_COMPILE  :=
MSG_LINK     :=
MSG_DONE     :=
BPFX         :=
ifneq ("$(DEBUG)","on")
 ifneq ("$(MCO_BUILD_RT_EVAL)","on")
  ifneq ("$(MCO_DIRECT_PTR)","on")
    GEN_FILE     := 1>>vxbuild.offs$(GEN_INMEM).lst 2>&1
  else
    GEN_FILE     := 1>>vxbuild.dptr$(GEN_INMEM).lst 2>&1
  endif
 else
  ifneq ("$(MCO_DIRECT_PTR)","on")
    GEN_FILE     := 1>>vxbuild.offs_eval$(GEN_INMEM).lst 2>&1
  else
    GEN_FILE     := 1>>vxbuild.dptr_eval$(GEN_INMEM).lst 2>&1
  endif
 endif
else
 GEN_FILE     := 1>>build-log 2>&1
endif

BSFX         := $(GEN_FILE)
endif

ifndef GENERATOR
LDOUTPUTF         = -o
LDCXXOUTPUTF      = -o
CCOUTPUTF         = -o
CXXOUTPUTF        = -o
else
#LDOUTPUTF         = ;echo "OUTLD: $@"; touch
#LDCXXOUTPUTF      = ;echo "OUTLD: $@"; touch
#CCOUTPUTF         = ; touch
#CXXOUTPUTF        = ; touch
LDOUTPUTF         = "\nOUT: "
LDCXXOUTPUTF      = "\nOUT: "
CCOUTPUTF         = 
CXXOUTPUTF        = 

endif
LDSTRIPF          = -s
LDCXXSTRIPF       = -s
CCCOMPF           = -c
CXXCOMPF          = -c
DEBUGF            = -g
OPTIMIZEF_SIZE    = -Os
OPTIMIZEF_SPEED   = -O3
ifeq ($(AUTODETECTED_PLATFORM),Darwin)
STATICF = 
SHAREDF           = -shared -undefined dynamic_lookup
else
STATICF           = #-static
SHAREDF           = -shared
endif
PICF              = -fPIC
X32F             := -m32
X64F             := -m64
CXXTEMPLATEDEPTHF:= -ftemplate-depth-99

# platform-dependent overrides
SETTINGS_APPLIED  = no
FAMILY_PLATFORM   = nix

ifeq ($(BUILD_PLATFORM),Linux)

ifeq ($(MCO_SIDE)-$(TARGET_OS_HINT),TARGET-eCos)
INSTALL_DIR    = $$(INSTALL_DIR)
ifeq ($(INSTALL_DIR),)
$(error Error: no path to ECOS installation given. Please type "make INSTALL_DIR=..." to set the path)
endif

include $(INSTALL_DIR)/include/pkgconf/ecos.mak
$(MCO_SIDE)_PLATFORM = $(ECOS_COMMAND_PREFIX)
endif # TARGET-eCos

ifeq ($(BUILD_PLATFORM),$($(MCO_SIDE)_PLATFORM))
$(MCO_SIDE)_ARCH  = $(shell echo $(BUILD_ARCH) | sed -e's/i.86/ix86/' -e's/^[xX]86.*_64/ix86_64/' -e's/^[xX]86.*/ix86/' -e's/sun4u/sparc64/' -e's/arm.*/arm/' -e's/sa110/arm/' -e's/mips.*/mips/')
else
$(MCO_SIDE)_ARCH  = $(shell echo $($(MCO_SIDE)_PLATFORM) | cut -d'-' -f1 | sed -e's/microblaze.*/microblaze/' -e's/ppc.*/ppc/' -e's/powerpc.*/ppc/' -e's/m68k.*/m68k/' -e's/i.86/ix86/' -e's/^[xX]86.*/ix86/' -e's/sun4u/sparc64/' -e's/arm.*/arm/' -e's/sa110/arm/' -e's/mips.*/mips/')
endif

ifneq ($($(MCO_SIDE)_CPUNO),1)
ARCH_FLAGS       += -D_SMP
endif

ifeq ($(findstring -mthumb,$($(MCO_SIDE)_FLAGS)),-mthumb)
ARCH_FLAGS       += -D_ARM_THUMB
endif

ifeq ($($(MCO_SIDE)_ARCH),ia64)
ARCH_FLAGS       += -D_ITANIUM
x64 ?= on
endif

ifeq ($($(MCO_SIDE)_ARCH),ix86_64)
x64 ?= on
ifeq ($(x64),on)
ARCH_FLAGS       += -D_Ix86
else
$(MCO_SIDE)_ARCH  = ix86
endif
endif

ifeq ($($(MCO_SIDE)_ARCH),ix86)
ARCH_FLAGS       += -D_Ix86
endif
ifeq ($($(MCO_SIDE)_ARCH),microblaze)
ARCH_FLAGS       += -D_MICROBLAZE
endif
ifeq ($($(MCO_SIDE)_ARCH),mips)
ARCH_FLAGS       += -D_MIPS32
ifneq ($(MCO_SIDE)-$(TARGET_OS_HINT),TARGET-NETOS)
TARGET_FLAGS     += -mips2
endif
endif
ifeq ($($(MCO_SIDE)_ARCH),ppc)
ARCH_FLAGS       += -D_PPC32
endif
ifeq ($($(MCO_SIDE)_ARCH),m68k)
ARCH_FLAGS       += -D_M68K
endif
ifeq ($($(MCO_SIDE)_ARCH),arm)
ARCH_FLAGS       += -D_ARM32
endif
ifeq ($($(MCO_SIDE)_ARCH),sparc64)
ARCH_FLAGS       += -D_SPARC64
endif
ifeq ($($(MCO_SIDE)_ARCH),sh4)
ARCH_FLAGS       += -D_SH4
endif
ifeq ($($(MCO_SIDE)_ARCH),tile)
ARCH_FLAGS       += -D_TILE
x64               = on
X32F             :=
X64F             :=
CXXTEMPLATEDEPTHF:=
PLATFORMLIB       = -lm -ltmc
endif


PLATFORM_ID    = -D_LINUX
PTHREADLIB     = 
PLATFORMLIB    = -lpthread 
SOCKETLIB      =
STATIC         = on

ifneq ($($(MCO_SIDE)_PLATFORM),$(BUILD_PLATFORM))
AR             = $($(MCO_SIDE)_PLATFORM)-ar
STRIP          = $($(MCO_SIDE)_PLATFORM)-strip
CC             = $($(MCO_SIDE)_PLATFORM)-gcc
CXX            = $($(MCO_SIDE)_PLATFORM)-g++
endif

ifeq ($(TARGET_OS_HINT),uCLinux)
PLATFORMLIB   += -lc
LDFLAGS       += -elf2flt
CFLAGS        += -D__UCLINUX
CXXFLAGS      += -D__UCLINUX
SOCFLAGS      += -D__UCLINUX
SOCXXFLAGS    += -D__UCLINUX
endif

ifeq ($(TARGET_OS_HINT),LynxOS)
PTHREADLIB     =
PLATFORMLIB    =
MCO_CFG_OVERRIDE += -DMCO_CONFIG_OVERRIDE_WCHAR
LDFLAGS       += -mthreads -L$(ENV_PREFIX)/lib/thread
LDCXXFLAGS  += -mthreads
CFLAGS        += -D__LYNX
CXXFLAGS      += -D__LYNX
SOCFLAGS      += -D__LYNX
SOCXXFLAGS    += -D__LYNX
endif

ifeq ($(MCO_SIDE)-$(TARGET_OS_HINT),TARGET-eCos)
ifeq ($(ECOS_COMMAND_PREFIX),)
ARCH_FLAGS     += -D_Ix86
endif

ifneq ($(wildcard $(INSTALL_DIR)/include/pkgconf/fs_*.h),)
ECOS_FS_F      := -D_ECOS_FS_PRESENT
else
ECOS_FS_F      := -D_ECOS_FS_ABSENT
NFS            := on
endif

ifneq ($(wildcard $(INSTALL_DIR)/include/pkgconf/net_freebsd_stack.h),)
ECOS_NET_F     := -D_ECOS_NET_PRESENT -D__ECOS
else
ECOS_NET_F     := -D_ECOS_NET_ABSENT
NN             := on
endif

ifneq ($(wildcard $(INSTALL_DIR)/include/pkgconf/memalloc.h),)
ECOS_MALLOC_F  := -D_ECOS_MALLOC_PRESENT
else
ECOS_MALLOC_F  := -D_ECOS_MALLOC_ABSENT
NMALLOC        := on
endif

#no shared memory support
NSHAREDMEM     := on

ifeq ($(PRJ_TYPE)-$(PRJ_F_CPP),APP-YES)
CC             := $(ECOS_COMMAND_PREFIX)g++
else
CC             := $(ECOS_COMMAND_PREFIX)gcc
endif
CXX            := $(ECOS_COMMAND_PREFIX)g++
AR             := $(ECOS_COMMAND_PREFIX)ar
STRIP          := $(ECOS_COMMAND_PREFIX)strip
ARCH_FLAGS     += -D_ECOS $(ECOS_FS_F) $(ECOS_NET_F) $(ECOS_MALLOC_F) -I$(INSTALL_DIR)/include $(ECOS_GLOBAL_CFLAGS)
LDFLAGS        := -nostartfiles -L$(INSTALL_DIR)/lib -Ttarget.ld $(ECOS_GLOBAL_LDFLAGS)
LDCXXFLAGS     := $(LDFLAGS)
PLATFORMLIB    :=
PLATFORM_ID     = -D_ECOS
MCO_CFG_OVERRIDE+= -DMCO_CONFIG_OVERRIDE_WCHAR
MCO_LIB_DYNAMIC = disabled
JAVA           :=off
ifeq ($(OPTIMIZEF_SPEED),-O3)
OPTIMIZEF_SPEED = -O2
endif
endif # TARGET-eCos

ifeq ($(MCO_SIDE)-$(TARGET_OS_HINT),TARGET-NETOS)
MCO_CFG_OVERRIDE += -DMCO_CONFIG_OVERRIDE_WCHAR
JAVA             :=off
NN               := on
X32F             :=
X64F             :=
MCO_LIB_DYNAMIC   = disabled
PLATFORM_ID       = -D_NETOS
STATICF          :=

ifeq ($(MCO_USE_ARCH_SFX),on)
BUILD_ARCH_SFX   := 
MCO_ARCH_SFX      = -$(TARGET_PLATFORM)
endif

ifeq ($(TARGET_PLATFORM:linux=),$(TARGET_PLATFORM))
# elf target
NFS              := on
ARCH_FLAGS       += -march=xlp -Wall -mabi=64 -EB -march=xlp -mabi=64
LDFLAGS          += -march=xlp -mabi=64
LDCXXFLAGS       += -march=xlp -mabi=64
SOLDFLAGS        += -march=xlp -mabi=64
else
# linux target
ifeq ($(HELINUX_INSTALL_DIR),)
$(error Error: Please specify HELINUX_INSTALL_DIR variable on make`s command line)
else
CFLAGS           += -I$(HELINUX_INSTALL_DIR)/include -pthread  -march=xlp -Wall -mabi=64 -EB 
CXXFLAGS         += -I$(HELINUX_INSTALL_DIR)/include -pthread  -march=xlp -Wall -mabi=64 -EB 
LDFLAGS          += -Wl,-u,linuxu_app_preinit -lc -lrt -lpthread -ldl -lnlm -lfuse -L$(HELINUX_INSTALL_DIR)/lib/ -march=xlp -Wall -mabi=64 -EB 
LDCXXFLAGS       += -Wl,-u,linuxu_app_preinit -lc -lrt -lpthread -ldl -lnlm -lfuse -L$(HELINUX_INSTALL_DIR)/lib/ -march=xlp -Wall -mabi=64 -EB 
SOLDFLAGS        += -Wl,-u,linuxu_app_preinit -lc -lrt -lpthread -ldl -lnlm -lfuse -L$(HELINUX_INSTALL_DIR)/lib/ -march=xlp -Wall -mabi=64 -EB 
endif #($(HELINUX_INSTALL_DIR),)
endif # ($(TARGET_PLATFORM:linux=),$(TARGET_PLATFORM))

PLATFORMLIB      :=
endif # TARGET-NETOS

## Setting up x64 defines --------------------------------

ifeq ($(gcov),on)
DEBUG=on
endif

ifeq ($(x64),on)
ifndef DEBUG
ifeq ($(OPTIMIZEF_SPEED),-O3)
OPTIMIZEF_SPEED = -O2
endif
endif

## if MontaVista CGE
ifneq ($(shell uname -a | grep mvlcge ),)

ifndef DEBUG
ifneq ($(OPTIMIZEF_SPEED),-O0)
OPTIMIZEF_SPEED = -O1
endif
endif

endif
## ----------------------------------------------

ifneq ($($(MCO_SIDE)_ARCH),ia64)

CXXFLAGS      += $(X64F)
CFLAGS        += $(X64F)
SOLDFLAGS     += $(X64F) 
SOCXXFLAGS    += $(X64F)
LDFLAGS       += $(X64F) 
LDCXXFLAGS    += $(X64F) 
endif 
 
else

ifeq ($(BUILD_PLATFORM),$($(MCO_SIDE)_PLATFORM))
CXXFLAGS      += $(X32F)
CFLAGS        += $(X32F)
SOLDFLAGS     += $(X32F) 
SOCXXFLAGS    += $(X32F)
LDFLAGS       += $(X32F) 
LDCXXFLAGS    += $(X32F) 
endif 

endif 

## -------------------------------------------------------

SETTINGS_APPLIED=yes

endif

ifeq ($(findstring CYGWIN_NT, $(BUILD_PLATFORM)),CYGWIN_NT)

ifeq ($(TARGET_OS_HINT),OS21)

PLATFORM_ID    = -D_OS21
PTHREADLIB     = 
PLATFORMLIB    =
SOCKETLIB      = 
COMMONFLAGS    = 
MCO_LIB_DYNAMIC= disabled

ifneq ($($(MCO_SIDE)_PLATFORM),$(BUILD_PLATFORM))
AR             = $($(MCO_SIDE)_PLATFORM)-ar
STRIP          = $($(MCO_SIDE)_PLATFORM)-strip
CC             = $($(MCO_SIDE)_PLATFORM)-gcc
CXX            = $($(MCO_SIDE)_PLATFORM)-g++
endif

else

CC             = gcc
CXX            = g++
PLATFORM_ID    = -D_LINUX
PTHREADLIB     = -lpthread
PLATFORMLIB    = 
SOCKETLIB      = 
COMMONFLAGS    += -I.
ARCH_FLAGS    += -D_Ix86

MCO_LIB_DYNAMIC= disabled
SHAREDF        = --dll 
SOFLAGS       += --export-all-symbols -lkernel32

FAMILY_PLATFORM=win
endif

SETTINGS_APPLIED=yes
endif

ifeq ($(BUILD_PLATFORM), WIN32)
CC             = cl.exe
CXX            = cl.exe

ifeq ($(x64), on)
LD             = link.exe /SUBSYSTEM:CONSOLE /MACHINE:X64
LDCXX          = link.exe /SUBSYSTEM:CONSOLE /MACHINE:X64
else
LD             = link.exe /SUBSYSTEM:CONSOLE /MACHINE:X86
LDCXX          = link.exe /SUBSYSTEM:CONSOLE /MACHINE:X86
endif

PLATFORM_ID    = -D_WIN32
PTHREADLIB     =
PLATFORMLIB    =  
SOCKETLIB      = ws2_32.lib
COMMONFLAGS    += /I. 

LDSTRIPF          = 
LDCXXSTRIPF       = 
CCCOMPF           = /c /FD /W2 /EHsc /MDd /D "_CRT_SECURE_NO_DEPRECATE" /D "WIN32" /D "_CONSOLE" /D "_VC80_UPGRADE=0x0600" /D "_MBCS" /Z7 /TC
CXXCOMPF          = /c /FD /W2 /EHsc /MDd /D "_CRT_SECURE_NO_DEPRECATE" /D "WIN32" /D "_CONSOLE" /D "_VC80_UPGRADE=0x0600" /D "_MBCS" /Z7 /TP 
DEBUGF            = /RTC1 /Od /D "_DEBUG"
OPTIMIZEF_SIZE    = /O1
OPTIMIZEF_SPEED   = /O2

LDOUTPUTF         =
LDCXXOUTPUTF      =
CCOUTPUTF         =
CXXOUTPUTF        =

MSVCLDOUTPUTF         = /NODEFAULTLIB:MSVCRT /OUT:
MSVCLDCXXOUTPUTF      = /NODEFAULTLIB:MSVCRT /OUT:
MSVCCCOUTPUTF         = /Fo
MSVCCXXOUTPUTF        = /Fo

CFLAGS            = $(COMMONFLAGS) /D "_WIN32" $(ARCH_FLAGS)
CXXFLAGS          = $(COMMONFLAGS) /D "_WIN32" $(ARCH_FLAGS) -fexceptions $(CXXTEMPLATEDEPTHF)

LIBSWITCH         = /LIBPATH:
LINKSWITCH        = 
LIBSUFFIX         = .lib
OBJSUFFIX         = .obj
EXESUFFIX         = .exe

MCO_INCLUDE       = /I$(MCO_ROOT)/include  /I$(MCO_ROOT)/include/ha
MCO_SQL_INCLUDE   = /I$(MCO_ROOT)/include/sql
MCO_ODBC_INCLUDE  = /I$(MCO_ROOT)/include/sql/odbc

ifneq (TEST_COMMENT,) 
MCO_INCLUDE       += /I$(MCO_ROOT)/tests/thlib
endif

LDFLAGS       = 
LDCXXFLAGS    =

MCO_LIB_DYNAMIC= disabled
SHAREDF        = 
#SOFLAGS       += --export-all-symbols -lkernel32

FAMILY_PLATFORM=win
SETTINGS_APPLIED=yes

endif 

ifeq ($(gcov),on)
ifneq ($(MCO_SIDE),HOST)
CFLAGS        += -fprofile-arcs -ftest-coverage
CXXFLAGS      += -fprofile-arcs -ftest-coverage
PLATFORMLIB   += -lgcov
endif
endif

ifeq ($(BUILD_PLATFORM),QNX)

CFLAGS        += $(MCO_INCLUDE)
CXXFLAGS      += $(MCO_INCLUDE)
PLATFORM_ID    = -D_QNX
PTHREADLIB     =
PLATFORMLIB    = -lm
SOCKETLIB      = -lsocket

ifeq ($($(MCO_SIDE)_PLATFORM),$(BUILD_PLATFORM))
else
AR             = nto$($(MCO_SIDE)_PLATFORM)-ar
STRIP          = nto$($(MCO_SIDE)_PLATFORM)-strip
CC             = nto$($(MCO_SIDE)_PLATFORM)-gcc
CXX            = nto$($(MCO_SIDE)_PLATFORM)-g++
endif

SETTINGS_APPLIED=yes
endif

ifeq ($(BUILD_PLATFORM),SunOS)
PLATFORM_ID    = -D_SOLARIS
PTHREADLIB     = -lpthread
PLATFORMLIB    = -lnsl
ifneq ($(MCO_SIDE),HOST)
OPTIMIZEF_SPEED = -O3
PLATFORMLIB   += -lrt
endif 

STATICF        =
SOCKETLIB      = -lsocket
SHAREDF        = -G
CFLAGS        += -DMCO_STRICT_ALIGNMENT
CXXFLAGS      += -DMCO_STRICT_ALIGNMENT

ifeq ($($(MCO_SIDE)_PLATFORM),$(BUILD_PLATFORM))
CC            := gcc
CXX           := g++
endif

BUILD_ARCH=$(shell uname -p)
$(MCO_SIDE)_ARCH  = $(shell echo $(BUILD_ARCH) | sed -e's/i.86/ix86/' -e's/^[xX]86.*/ix86/' -e's/sun4u/sparc64/' )

ifeq ($($(MCO_SIDE)_ARCH),ix86)
ARCH_FLAGS    += -D_Ix86
SOLDFLAGS     += -lrt
endif

ifeq ($($(MCO_SIDE)_ARCH),sparc)
ARCH_FLAGS    += -D_SPARC
CFLAGS        += -Wa,-xarch=v8plus
CXXFLAGS      += -Wa,-xarch=v8plus
SOCFLAGS      += -Wa,-xarch=v8plus
SOCXXFLAGS    += -Wa,-xarch=v8plus
endif

ifeq ($(x64),on)
CXXFLAGS      += $(X64F)
CFLAGS        += $(X64F)
SOLDFLAGS     += $(X64F) 
SOCXXFLAGS    += $(X64F)
LDFLAGS       += $(X64F) 
LDCXXFLAGS    += $(X64F) 
else
CXXFLAGS      += $(X32F)
CFLAGS        += $(X32F)
SOLDFLAGS     += $(X32F) 
SOCXXFLAGS    += $(X32F)
LDFLAGS       += $(X32F) 
LDCXXFLAGS    += $(X32F) 
endif 

ifeq ($($(MCO_SIDE)_PLATFORM),$(BUILD_PLATFORM))
SETTINGS_APPLIED=yes
endif

endif

ifeq ($(BUILD_PLATFORM),HP-UX)

ifeq ($(BUILD_ARCH),ia64)
ARCH_FLAGS     += -D_ITANIUM
endif

PLATFORM_ID    := -D_HPUX
PTHREADLIB     := -lpthread
PLATFORMLIB    := -lrt 
SOCKETLIB      := 
SHAREDF        := -lrt -shared
OPTIMIZEF_SPEED:= -O2
PICF            =
HOST_FLAGS     += -DHAVE_ALLOCA_H
CXXFLAGS       = $(COMMONFLAGS) $(PLATFORM_ID) $(ARCH_FLAGS) -DMCO_STRICT_ALIGNMENT
# -D_XOPEN_SOURCE_EXTENDED
# -D_INCLUDE__STDC_A1_SOURCE 
CFLAGS         = $(COMMONFLAGS) $(PLATFORM_ID) $(ARCH_FLAGS) -DMCO_STRICT_ALIGNMENT
# -D_XOPEN_SOURCE_EXTENDED 
 
# if compiler was not defined prefer native compiler instead of GNU
ifeq ($(ENV_CC),)
CC             := cc
endif
ifeq ($(ENV_CXX),)
CXX            := aCC
endif

ifeq ($(CC),gcc)
ifeq ($(x64),on)
CFLAGS        += -mlp64
SOLDFLAGS     += -mlp64 
LDFLAGS       += -mlp64 
endif 
endif

ifeq ($(CC),g++)
ifeq ($(x64),on)
CXXFLAGS      += -mlp64
SOCXXFLAGS    += -mlp64
LDCXXFLAGS    += -mlp64 
endif 
endif

ifeq ($(CC),cc)
STATICF        = -N
SHAREDF        = -b -lrt
CFLAGS        += +z -Ae -Agcc

ifeq ($(x64),on)
LD	       = $(CXX) # using aCC as linker for all x64 build on HPUX
CFLAGS        += +DD64 
SOLDFLAGS     += +DD64 
LDFLAGS       += +DD64 
else
CFLAGS        += +DD32 
SOLDFLAGS     += +DD32 
LDFLAGS       += +DD32 
endif

ifneq ($(BUILD_ARCH),ia64)
CFLAGS        += +DAportable  # +DAportable is applicable for PA-RISK hardware only 
endif
endif

ifeq ($(CXX),aCC)
STATICF        = -N
SHAREDF        = -b -lrt
CXXFLAGS      += -Ag++

ifeq ($(x64),on)
LDXX	       = $(CXX)
CXXFLAGS      += +DD64 
SOCXXFLAGS    += +DD64
LDCXXFLAGS    += +DD64 
else
CXXFLAGS      += +DD32 
SOCXXFLAGS    += +DD32
LDCXXFLAGS    += +DD32
endif
endif

ifeq ($($(MCO_SIDE)_PLATFORM),$(BUILD_PLATFORM))
SETTINGS_APPLIED=yes
endif

endif

ifeq ($(BUILD_PLATFORM),ADS)
PLATFORM_ID    = -D_ADS
PTHREADLIB     =
PLATFORMLIB    =
SOCKETLIB      =
CC             = armcc
CXX            = armcpp
AR             = armar
MCO_LIB_DYNAMIC= disabled
MCO_SHAREDMEM  = disabled
OPTIMIZEF_SPEED= -O2
COMMONFLAGS   += -DMCO_CFG_EXTENDABLE_DIRTY_PAGE_BITMAP=0

SETTINGS_APPLIED=yes
endif


## Setting up x64 defines --------------------------------

ifeq ($(x64),on)
ifeq ($(BUILD_PLATFORM), WIN32)
ARCH_FLAGS        += /D "MCO_PLATFORM_X64"
else
ARCH_FLAGS        += -DMCO_PLATFORM_X64
endif
endif

## -------------------------------------------------------

ifneq ($(SETTINGS_APPLIED),yes)
$(error Unable to apply build settings)
endif

## --------------------------------------------------------------------
## Creating special file for build system if needed (x64|x32_arch_binaries)
ifeq ($(SHOW_X64_ARCH),on)

## Creating arch file
ifeq ($(shell rm -rf $(MCO_ROOT)/x64_arch_binaries; rm -rf $(MCO_ROOT)/x32_arch_binaries),dummy)
endif

ifeq ($(x64),on)
ifeq ($(shell touch $(MCO_ROOT)/x64_arch_binaries),dummy)
endif
else
ifeq ($(shell touch $(MCO_ROOT)/x32_arch_binaries),dummy)
endif
endif

endif
## -------------------------------------------------------------------- 

ifeq ($(MCO_LARGE_DATABASE_SUPPORT),on)
ifeq ($(BUILD_PLATFORM), WIN32)
MCO_CFG_OVERRIDE       += /D "MCO_LARGE_DATABASE_SUPPORT"
else
MCO_CFG_OVERRIDE       += -DMCO_LARGE_DATABASE_SUPPORT
endif
endif

## Dealing with debug and optimization --------------------------------
ifeq ($(OPTIMIZE),size)
OPTIMIZEFLAGS  = $(OPTIMIZEF_SIZE)
else
OPTIMIZEFLAGS  = $(OPTIMIZEF_SPEED)
endif
ifdef DEBUG
ifeq ($(BUILD_PLATFORM), WIN32)
DEBUGFLAGS     = $(DEBUGF) /D "MCO_DEBUG_DISK_CACHE"
LDFLAGS       += /DEBUG
else
DEBUGFLAGS     = $(DEBUGF) -DMCO_DEBUG_DISK_CACHE
endif
OPTIMIZEFLAGS  = 
else
LDCXXFLAGS    += $(LDCXXSTRIPF)
LDFLAGS       += $(LDSTRIPF)
SOFLAGS       += $(LDSTRIPF)
endif

## Pretty build log ---------------------------------------------------
ifndef SHOW_BUILD_LOG
QUIET_COMPILE  =@echo Compiling $< ...
QUIET_FINALIZE =@echo Building $(MCO_TITLE) ... ; \
cat $(MCO_BUILD_LOG_DIR)/$(MCO_TITLE)$(MCO_LOGFILE_SFX) | grep 'MCO-MESSAGE' | sed -e 's/.*MCO-MESSAGE *//'
QUIET_PREFIX   =@
#QUIET_SUFFIX   =1>>$(MCO_BUILD_LOG_DIR)/$(MCO_TITLE)$(MCO_LOGFILE_SFX) 2>>$(MCO_BUILD_LOG_DIR)/$(MCO_TITLE)$(MCO_LOGFILE_SFX)
QUIET_SUFFIX   = >>$(MCO_BUILD_LOG_DIR)/$(MCO_TITLE)$(MCO_LOGFILE_SFX) 2>&1
MAKEFLAGS     += --no-print-directory
else
QUIET_COMPILE  =
QUIET_FINALIZE =
QUIET_PREFIX   =
QUIET_SUFFIX   = 
#QUIET_SUFFIX   = 2>&1 | tee -a $(MCO_BUILD_LOG_DIR)/$(MCO_TITLE)$(MCO_LOGFILE_SFX)
endif

ifdef GENERATOR
MSG_COMPILE  :=
MSG_LINK     :=
MSG_DONE     :=
BSFX         :=
BSFX         = $(GEN_FILE)
QUIET_SUFFIX = $(GEN_FILE)
PLATFORM_ID  = -D_VXWORKS
endif


## Compilation rules -------------------------------------------------
ifneq ($(PRJ_TYPE),APP)

MCO_OBJ_DIR      := $(addsuffix $(MCO_ARCH_SFX),$(MCO_OBJ_DIR))

objdir = $(addprefix $(MCO_OBJ_DIR)/,$(filter %$(OBJSUFFIX),$(notdir $(1:%.c=%$(MCO_OBJ_VSFX)$(OBJSUFFIX))))) $(addprefix $(MCO_OBJ_DIR)/,$(filter %$(OBJSUFFIX),$(notdir $(1:%.cpp=%$(MCO_OBJ_VSFX)$(OBJSUFFIX)))))

OBJ = $(call objdir,$(SRC))
ifeq ($(MCO_LIB_DYNAMIC),enabled)
OBJ_L         = $(OBJ:.o=.lo)
 else
OBJ_L         =
endif

.make_obj_dir:
	@sh -c "if [ ! -d $(MCO_BUILD_LOG_DIR) ] ; then mkdir $(MCO_BUILD_LOG_DIR); exit 0; fi"
	@echo "    PROJECT NAME: $(MCO_TITLE)">$(MCO_BUILD_LOG_DIR)/$(MCO_TITLE)$(MCO_LOGFILE_SFX)
ifndef SHOW_BUILD_LOG
	@echo " RUNTIME_VERSION: $(shell $(MCO_VERSION))">$(MCO_BUILD_LOG_DIR)/$(MCO_TITLE)$(MCO_LOGFILE_SFX)
	@echo "        MCO_SIDE: $(MCO_SIDE)">>$(MCO_BUILD_LOG_DIR)/$(MCO_TITLE)$(MCO_LOGFILE_SFX)
	@echo "             SRC: $(SRC)">>$(MCO_BUILD_LOG_DIR)/$(MCO_TITLE)$(MCO_LOGFILE_SFX)
	@echo "          MCO_RT: $(MCO_RT)">>$(MCO_BUILD_LOG_DIR)/$(MCO_TITLE)$(MCO_LOGFILE_SFX)
	@echo "              CC: $(CC)">>$(MCO_BUILD_LOG_DIR)/$(MCO_TITLE)$(MCO_LOGFILE_SFX)
	@echo "             CXX: $(CXX)">>$(MCO_BUILD_LOG_DIR)/$(MCO_TITLE)$(MCO_LOGFILE_SFX)
	@echo "          CFLAGS: $(CFLAGS)">>$(MCO_BUILD_LOG_DIR)/$(MCO_TITLE)$(MCO_LOGFILE_SFX)
	@echo "        CXXFLAGS: $(CXXFLAGS)">>$(MCO_BUILD_LOG_DIR)/$(MCO_TITLE)$(MCO_LOGFILE_SFX)
	@echo "              LD: $(LD)">>$(MCO_BUILD_LOG_DIR)/$(MCO_TITLE)$(MCO_LOGFILE_SFX)
	@echo "           LDCXX: $(LDCXX)">>$(MCO_BUILD_LOG_DIR)/$(MCO_TITLE)$(MCO_LOGFILE_SFX)
	@echo "         LDFLAGS: $(LDFLAGS)">>$(MCO_BUILD_LOG_DIR)/$(MCO_TITLE)$(MCO_LOGFILE_SFX)
	@echo "      LDCXXFLAGS: $(LDCXXFLAGS)">>$(MCO_BUILD_LOG_DIR)/$(MCO_TITLE)$(MCO_LOGFILE_SFX)
	@echo "MCO_CFG_OVERRIDE: $(MCO_CFG_OVERRIDE)">>$(MCO_BUILD_LOG_DIR)/$(MCO_TITLE)$(MCO_LOGFILE_SFX)
	@echo "     MCO_INCLUDE: $(MCO_INCLUDE)">>$(MCO_BUILD_LOG_DIR)/$(MCO_TITLE)$(MCO_LOGFILE_SFX)
	@echo "      PTHREADLIB: $(PTHREADLIB)">>$(MCO_BUILD_LOG_DIR)/$(MCO_TITLE)$(MCO_LOGFILE_SFX)
	@echo "     PLATFORMLIB: $(PLATFORMLIB)">>$(MCO_BUILD_LOG_DIR)/$(MCO_TITLE)$(MCO_LOGFILE_SFX)
	@echo "       SOCKETLIB: $(SOCKETLIB)">>$(MCO_BUILD_LOG_DIR)/$(MCO_TITLE)$(MCO_LOGFILE_SFX)
	@echo "            PATH: $(PATH)">>$(MCO_BUILD_LOG_DIR)/$(MCO_TITLE)$(MCO_LOGFILE_SFX)
	@echo >>$(MCO_BUILD_LOG_DIR)/$(MCO_TITLE)$(MCO_LOGFILE_SFX)
	@echo
	@echo Project: $(MCO_TITLE) $(GEN_FILE)
endif
	@sh -c "if [ ! -d $(MCO_OBJ_DIR) ] ; then mkdir $(MCO_OBJ_DIR); exit 0; fi"
	@sh -c "if [ ! -d $(MCO_BIN) ] ; then mkdir $(MCO_BIN); exit 0; fi"
ifeq ($(MCO_SIDE),TARGET)
	@sh -c "if [ ! -d $(MCO_BIN_SO) ] ; then mkdir $(MCO_BIN_SO); exit 0; fi"
	@sh -c "if [ -d $(MCO_ODBC) -a ! -d $(MCO_ODBC_BIN) ] ; then mkdir $(MCO_ODBC_BIN); exit 0; fi"
	@sh -c "if [ -d $(MCO_ODBC) -a ! -d $(MCO_ODBC_BIN_SO) ] ; then mkdir $(MCO_ODBC_BIN_SO); exit 0; fi"
endif

.clear_obj_dir:
	@sh -c "if [ -d $(MCO_OBJ_DIR) ] ; then rm -f $(MCO_OBJ_DIR)/*; exit 0; fi"

$(MCO_OBJ_DIR)/%$(MCO_OBJ_VSFX)$(OBJSUFFIX): %.cpp
	$(QUIET_COMPILE)
	$(QUIET_PREFIX)$(CXX) $(CXXFLAGS) $(MCO_CFG_OVERRIDE) $(MCO_INCLUDE) $(CXXCOMPF) $< $(CXXOUTPUTF) $(MSVCCXXOUTPUTF)$@ $(QUIET_SUFFIX)

$(MCO_OBJ_DIR)/%$(MCO_OBJ_VSFX).lo: %.cpp
	$(QUIET_COMPILE)
	$(QUIET_PREFIX)$(CXX) $(CXXFLAGS) $(SOCXXFLAGS) $(MCO_CFG_OVERRIDE) $(MCO_INCLUDE) $(CXXCOMPF) $< $(CXXOUTPUTF) $(MSVCCXXOUTPUTF)$@ $(QUIET_SUFFIX)

$(MCO_OBJ_DIR)/%$(MCO_OBJ_VSFX)$(OBJSUFFIX): %.c
	$(QUIET_COMPILE)
	$(QUIET_PREFIX)$(CC) $(CFLAGS) $(MCO_CFG_OVERRIDE) $(MCO_INCLUDE) $(CCCOMPF) $< $(CCOUTPUTF) $(MSVCCCOUTPUTF)$@ $(QUIET_SUFFIX)

$(MCO_OBJ_DIR)/%$(MCO_OBJ_VSFX)$(OBJSUFFIX): $(MCO_OBJ_DIR)/%.c
	$(QUIET_COMPILE)
	$(QUIET_PREFIX)$(CC) $(CFLAGS) $(MCO_CFG_OVERRIDE) $(MCO_INCLUDE) $(CCCOMPF) $< $(CCOUTPUTF) $(MSVCCCOUTPUTF)$@ $(QUIET_SUFFIX)

$(MCO_OBJ_DIR)/%$(MCO_OBJ_VSFX).lo: %.c
	$(QUIET_COMPILE)
	$(QUIET_PREFIX)$(CC) $(CFLAGS) $(SOCFLAGS) $(MCO_CFG_OVERRIDE) $(MCO_INCLUDE) $(CCCOMPF) $< $(CCOUTPUTF) $(MSVCCCOUTPUTF)$@ $(QUIET_SUFFIX)

$(MCO_OBJ_DIR)/%$(MCO_OBJ_VSFX)$(OBJSUFFIX): $(MCO_ROOT)/platform/%.c
	$(QUIET_COMPILE)
	$(QUIET_PREFIX)$(CC) $(CFLAGS) $(MCO_CFG_OVERRIDE) $(MCO_INCLUDE) $(CCCOMPF) $< $(CCOUTPUTF) $(MSVCCCOUTPUTF)$@ $(QUIET_SUFFIX)

$(MCO_OBJ_DIR)/%$(MCO_OBJ_VSFX)$(OBJSUFFIX): $(MCO_ROOT)/src/%.c
	$(QUIET_COMPILE)
	$(QUIET_PREFIX)$(CC) $(CFLAGS) $(MCO_CFG_OVERRIDE) $(MCO_INCLUDE) $(CCCOMPF) $< $(CCOUTPUTF) $(MSVCCCOUTPUTF)$@ $(QUIET_SUFFIX)

ifndef SHOW_BUILD_LOG
.remove_obj_dir:
	@echo Removing temporary files for $(MCO_TITLE) ...
	@sh -c "if [ -d $(MCO_OBJ_DIR) ] ; then rm -rf $(MCO_OBJ_DIR); else exit 0; fi"
	@sh -c "if [ -d $(addsuffix $(DISK_SFX),$(MCO_OBJ_DIR)) ] ; then rm -rf $(addsuffix $(DISK_SFX),$(MCO_OBJ_DIR)); else exit 0; fi"
	@sh -c "if [ -d $(addsuffix $(MEMORY_SFX),$(MCO_OBJ_DIR)) ] ; then rm -rf $(addsuffix $(MEMORY_SFX),$(MCO_OBJ_DIR)); else exit 0; fi"
	@sh -c "rm -rf vxbuild*.lst"
else
.remove_obj_dir:
	sh -c "if [ -d $(MCO_OBJ_DIR) ] ; then rm -rf $(MCO_OBJ_DIR); else exit 0; fi"
	sh -c "if [ -d $(addsuffix $(DISK_SFX),$(MCO_OBJ_DIR)) ] ; then rm -rf $(addsuffix $(DISK_SFX),$(MCO_OBJ_DIR)); else exit 0; fi"
	sh -c "if [ -d $(addsuffix $(MEMORY_SFX),$(MCO_OBJ_DIR)) ] ; then rm -rf $(addsuffix $(MEMORY_SFX),$(MCO_OBJ_DIR)); else exit 0; fi"
	sh -c "rm -rf vxbuild*.lst"
endif

.show_compile_settings:
	@echo MCO_SIDE=$(MCO_SIDE)
	@echo $(MCO_SIDE)_ARCH=$($(MCO_SIDE)_ARCH)
	@echo SRC=$(SRC)
	@echo OBJ=$(OBJ)
	@echo CC=$(CC)
	@echo CFLAGS=$(CFLAGS)
	@echo LDFLAGS=$(LDFLAGS)
	@echo CXX=$(CXX)
	@echo CXXFLAGS=$(CXXFLAGS)
	@echo LCXXDFLAGS=$(LCXXDFLAGS)
	@echo MCOCOMP=$(MCOCOMP)
	@echo MCO_INCLUDE=$(MCO_INCLUDE)
	@echo MCO_CFG_OVERRIDE=$(MCO_CFG_OVERRIDE)
	@echo MCO_OBJ_DIR=$(MCO_OBJ_DIR)
	@echo MCO_BIN=$(MCO_BIN)
	@echo MCO_BUILDRT_SFX=$(MCO_BUILDRT_SFX)
	@echo MCO_CFG_SUFFIX_A=$(MCO_CFG_SUFFIX_A)
	@echo MCO_CFG_SUFFIX_SO=$(MCO_CFG_SUFFIX_SO)
endif # PRJ_TYPE!=APP

### --- New samples 4.x ----------------------------------------------
	
ifeq ($(PRJ_TYPE),APP)

MCO_SAMPLES := $(MCO_ROOT)/samples
# resulting binary file
PRJ_COMMON_SRC :=
PRJ_BIN_FILE    = $(MCO_ROOT)/$(PRJ_SIDE_DIR)/$(PRJ_BIN_DIR)/$(PRJ_TITLE)

ifeq ($(x64),on)
PRJ_MCOCOMP_F_X32_X64 := -x64
else
PRJ_MCOCOMP_F_X32_X64 := -x32
endif

ifeq ($(PRJ_SIDE),TARGET)
 PRJ_SIDE_DIR  := target
else 
 ifeq ($(PRJ_SIDE),HOST)
  PRJ_SIDE_DIR  := host
 else 
  ifeq ($(PRJ_SIDE),NONE)
   PRJ_SIDE_DIR  := # do not link
  else # PRJ_SIDE != (HOST|TARGET|NONE)
   $(error Variable PRJ_SIDE. Invalid value)
  endif
 endif
endif

ifeq ($(PRJ_DIRECTPTR),YES)
 ifeq ($(BUILD_PLATFORM), WIN32)
 CFLAGS        += /D "MCO_CFG_USE_DIRECT_POINTERS"
 else
 CFLAGS        += -DMCO_CFG_USE_DIRECT_POINTERS
 endif
 ifeq ($(strip $(MCO_PACKAGE_REGL)),yes)
  PRJ_BIN_DIR   := bin.dptr
 else
  PRJ_BIN_DIR   := bin
 endif
else 
 ifeq ($(PRJ_DIRECTPTR),NO)
  PRJ_BIN_DIR   := bin
 else 
  ifeq ($(PRJ_DIRECTPTR),AUTO)
   PRJ_BIN_DIR   := bin
   ifeq ($(strip $(MCO_PACKAGE_REGL)),no)
     ifeq ($(BUILD_PLATFORM), WIN32)
     CFLAGS        += /D "MCO_CFG_USE_DIRECT_POINTERS"
     else
     CFLAGS        += -DMCO_CFG_USE_DIRECT_POINTERS
     endif
   endif
  else  # PRJ_DIRECTPTR != (YES|NO|AUTO)
   $(error Variable PRJ_DIRECTPTR. Invalid value "$(PRJ_DIRECTPTR)")
  endif
 endif
endif

ifeq ($(MCO_USE_ARCH_SFX),on)
PRJ_BIN_DIR     := $(PRJ_BIN_DIR)$(MCO_ARCH_SFX)
endif

# configuration analysis
PRJ_RUNTIME   := NONE  # link runtime wrapper library only if needed

ifneq ($(TEST_COMMENT),)
  PRJ_RUNTIME    := AUTO
  PRJ_COMMON_SRC := $(MCO_ROOT)/tests/thlib/thlib.c
  PRJ_COMMON_DEP := $(MCO_ROOT)/tests/thlib/thlib.h
  ifeq ($(PRJ_STORAGE),MEMORY)
	PRJ_LVT  := $(LINKSWITCH)mcolib $(LINKSWITCH)mcovtmem
  else 
    ifeq ($(PRJ_STORAGE),PERSISTENT)
	  PRJ_LVT  := $(LINKSWITCH)mcolib $(LINKSWITCH)mcovtdsk
      PRJ_SAMPLE_REQ_FS:=DISK
      ifeq ($(BUILD_PLATFORM), WIN32)
        COMMONFLAGS    += /D "DISK_DATABASE"
      else
        COMMONFLAGS    += -DDISK_DATABASE
      endif
    else 
      ifeq ($(PRJ_STORAGE),NONE)
        PRJ_LVT  := 
      else 
        $(error Variable PRJ_STORAGE. Invalid value)
      endif
    endif
  endif	  
else
  ifeq ($(PRJ_STORAGE),MEMORY)
   PRJ_LVT  := $(LINKSWITCH)mcolib $(LINKSWITCH)mcovtmem
   PRJ_RUNTIME    := AUTO
   PRJ_COMMON_SRC := $(MCO_SAMPLES)/common/common.c $(MCO_SAMPLES)/common/initos.c
   PRJ_COMMON_DEP := $(MCO_SAMPLES)/common/common.h $(MCO_SAMPLES)/common/initos.h  
  else 
   ifeq ($(PRJ_STORAGE),PERSISTENT)
    PRJ_LVT  := $(LINKSWITCH)mcolib $(LINKSWITCH)mcovtdsk
    PRJ_RUNTIME    := AUTO
    PRJ_COMMON_SRC := $(MCO_SAMPLES)/common/common.c $(MCO_SAMPLES)/common/initos.c
    PRJ_COMMON_DEP := $(MCO_SAMPLES)/common/common.h $(MCO_SAMPLES)/common/initos.h
    PRJ_SAMPLE_REQ_FS:=DISK
    # for SQL samples only. Will be changed to runtime_info check
    ifeq ($(BUILD_PLATFORM), WIN32)
      COMMONFLAGS    += /D "DISK_DATABASE"
    else
      COMMONFLAGS    += -DDISK_DATABASE
    endif
   else 
     ifeq ($(PRJ_STORAGE),NONE)
       PRJ_LVT  := 
     else 
       $(error Variable PRJ_STORAGE. Invalid value)
     endif
   endif
  endif
endif

ifeq ($(PRJ_MEMDEV),CONV)
 PRJ_LMEM  :=$(LINKSWITCH)mcom$(strip $(DEFAULT_MEM_C$(subst -D,,$(PLATFORM_ID)))) 
else 
 ifeq ($(PRJ_MEMDEV),SHARED)
  PRJ_LMEM  :=$(LINKSWITCH)mcom$(strip $(DEFAULT_MEM_S$(subst -D,,$(PLATFORM_ID)))) 
 else 
  ifeq ($(PRJ_MEMDEV),NONE)
   PRJ_LMEM  := # no memory driver
  else 
   $(error Variable PRJ_MEMDEV. Invalid value)
  endif
 endif
endif

ifeq ($(PRJ_STORAGEDEV),AUTO)
 PRJ_LFS  := $(MCO_FS_LIB)
else 
 ifeq ($(PRJ_STORAGEDEV),NONE)
  PRJ_LFS  := 
 else 
  PRJ_LFS  := $(PRJ_STORAGEDEV)
 endif
endif

ifeq ($(PRJ_SYNC),AUTO)
 ifeq ($(PRJ_MEMDEV),CONV)
  PRJ_LSYNC :=$(LINKSWITCH)mcos$(strip $(DEFAULT_SYNC_C$(subst -D,,$(PLATFORM_ID)))) 
 else 
  ifeq ($(PRJ_MEMDEV),SHARED)
   PRJ_LSYNC :=$(LINKSWITCH)mcos$(strip $(DEFAULT_SYNC_S$(subst -D,,$(PLATFORM_ID)))) 
  endif
 endif 
else 
 ifeq ($(PRJ_SYNC),NONE) 
  PRJ_LSYNC := # no sync driver
 else 
  PRJ_LSYNC:=$(LINKSWITCH)mcos$(PRJ_SYNC) # manual settings
 endif
endif

ifeq ($(PRJ_TRANSMGR),AUTO)
 PRJ_LTMGR:= $(LINKSWITCH)mcotmursiw $(LINKSWITCH)mcolib
else 
 ifeq ($(PRJ_TRANSMGR),MURSIW)
  PRJ_LTMGR:= $(LINKSWITCH)mcotmursiw $(LINKSWITCH)mcolib
 else 
  ifeq ($(PRJ_TRANSMGR),MVCC)
   PRJ_LTMGR:= $(LINKSWITCH)mcotmvcc $(LINKSWITCH)mcolib
  else 
   ifeq ($(PRJ_TRANSMGR),EXCLUSIVE)
    PRJ_LTMGR:= $(LINKSWITCH)mcotexcl $(LINKSWITCH)mcolib
   else 
    ifeq ($(PRJ_TRANSMGR),MURSIW-READ)
     PRJ_LTMGR:= -lmcotread -lmcolib
    else

     ifeq ($(PRJ_TRANSMGR),NONE)
      PRJ_LTMGR:= 
     else 
      $(error Variable PRJ_TRANSMGR. Invalid value)
     endif
    endif
   endif
  endif
 endif
endif

ifeq ($(PRJ_RPC),CLIENT)
 PRJ_LRPC := $(LINKSWITCH)mcorpc $(SOCKETLIB)
 PRJ_COMMON_SRC := $(MCO_SAMPLES)/common/initos.c
 PRJ_COMMON_DEP := $(MCO_SAMPLES)/common/initos.h
 PRJ_SAMPLE_REQ_NET:=NET
else 
 ifeq ($(PRJ_RPC),SERVER)
  PRJ_LRPC := $(LINKSWITCH)mcorpc $(SOCKETLIB)
  PRJ_COMMON_SRC := $(MCO_SAMPLES)/common/initos.c
  PRJ_COMMON_DEP := $(MCO_SAMPLES)/common/initos.h
  PRJ_SAMPLE_REQ_NET:=NET
 else 
  ifeq ($(PRJ_RPC),NONE)
   PRJ_LRPC :=
  else 
   $(error Variable PRJ_RPC. Invalid value)
  endif
 endif
endif

ifeq ($(PRJ_F_SQL),LOCAL)
 PRJ_LSQL := $(LINKSWITCH)mcoapi $(LINKSWITCH)mcosql
 PRJ_MCOCOMP_F_SQL := -sql
 PRJ_COMMON_SRC += $(MCO_SAMPLES)/common/commonSQL.c
 PRJ_COMMON_DEP += $(MCO_SAMPLES)/common/commonSQL.h
 PRJ_SAMPLE_REQ_FS :=DISK
 ifeq ($(PRJ_STORAGE),PERSISTENT)
  PRJ_LSQL += $(LINKSWITCH)mcoseridsk
 else
  PRJ_LSQL += $(LINKSWITCH)mcoseri
 endif
else 
 ifeq ($(PRJ_F_SQL),REMOTE)
  PRJ_LSQL := $(LINKSWITCH)mcoapi $(LINKSWITCH)mcosql $(LINKSWITCH)mcorsql $(SOCKETLIB)
  PRJ_MCOCOMP_F_SQL := -sql
  PRJ_COMMON_SRC += $(MCO_SAMPLES)/common/commonSQL.c
  PRJ_COMMON_DEP += $(MCO_SAMPLES)/common/commonSQL.h
  PRJ_SAMPLE_REQ_NET:=NET
  ifeq ($(PRJ_STORAGE),PERSISTENT)
   PRJ_LSQL += $(LINKSWITCH)mcoseridsk
  else
   PRJ_LSQL += $(LINKSWITCH)mcoseri
  endif
 else 
  ifeq ($(PRJ_F_SQL),NO)
   PRJ_LSQL :=
   PRJ_MCOCOMP_F_SQL :=
  else 
   $(error Variable PRJ_F_SQL. Invalid value)
  endif
 endif
endif

ifeq ($(PRJ_HA),NONE)
 PRJ_LHA  :=
else 
 PRJ_SAMPLE_REQ_NET:=NET
 ifeq ($(PRJ_HA),TCP)
  PRJ_LHA  := $(LINKSWITCH)mcoha $(LINKSWITCH)mconwtcp $(SOCKETLIB)
  PRJ_COMMON_SRC += $(MCO_SAMPLES)/common/commonha.c
  PRJ_COMMON_DEP += $(MCO_SAMPLES)/common/commonha.h
  ifeq ($(BUILD_PLATFORM), WIN32)
  CFLAGS += /D "CFG_TCP"
  else
  CFLAGS += -DCFG_TCP
  endif
  ifeq ($(BUILD_PLATFORM), WIN32)
  CXXFLAGS += /D "CFG_TCP"
  else
  CXXFLAGS += -DCFG_TCP
  endif
 else 
  ifeq ($(PRJ_HA),UDP)
   PRJ_LHA  := $(LINKSWITCH)mcoha $(LINKSWITCH)mconwudp $(SOCKETLIB)
   PRJ_COMMON_SRC += $(MCO_SAMPLES)/common/commonha.c
   PRJ_COMMON_DEP += $(MCO_SAMPLES)/common/commonha.h
   ifeq ($(BUILD_PLATFORM), WIN32)
   CFLAGS += /D "CFG_UDP"
   CXXFLAGS += /D "CFG_UDP"
   else
   CFLAGS += -DCFG_UDP
   CXXFLAGS += -DCFG_UDP
   endif
  else 
   ifeq ($(PRJ_HA),PIPES)
    PRJ_LHA  := $(LINKSWITCH)mcoha $(LINKSWITCH)mconwpipes
    PRJ_COMMON_SRC += $(MCO_SAMPLES)/common/commonha.c
    PRJ_COMMON_DEP += $(MCO_SAMPLES)/common/commonha.h
    ifeq ($(BUILD_PLATFORM), WIN32)
    CFLAGS += /D "CFG_PIPES"
    CXXFLAGS += /D "CFG_PIPES"
    else
    CFLAGS += -DCFG_PIPES
    CXXFLAGS += -DCFG_PIPES
    endif
   else 
    $(error Variable PRJ_HA. Invalid value)
   endif
  endif
 endif
 ifeq ($(PRJ_STORAGE),PERSISTENT)
  PRJ_LHA += $(LINKSWITCH)mcoseridsk
 else
  PRJ_LHA += $(LINKSWITCH)mcoseri
 endif
endif

ifeq ($(PRJ_F_LOG),YES)
 PRJ_LLOG := $(LINKSWITCH)mcolog
 PRJ_SAMPLE_REQ_FS:=DISK
 ifeq ($(PRJ_STORAGE),PERSISTENT)
  PRJ_LLOG += $(LINKSWITCH)mcoseridsk
 else
  PRJ_LLOG += $(LINKSWITCH)mcoseri
 endif
else 
 ifeq ($(PRJ_F_LOG),NO)
  PRJ_LLOG :=
 else 
  $(error Variable PRJ_F_LOG. Invalid value)
 endif
endif

ifeq ($(PRJ_F_SERI),YES)
 ifeq ($(PRJ_STORAGE),PERSISTENT)
  PRJ_LSERI += $(LINKSWITCH)mcoseridsk
 else
  PRJ_LSERI += $(LINKSWITCH)mcoseri
 endif
else 
 ifeq ($(PRJ_F_SERI),NO)
  PRJ_LSERI :=
 else 
  $(error Variable PRJ_F_SERI. Invalid value)
 endif
endif

ifeq ($(PRJ_F_DBCALC),YES)
 PRJ_LDBC := $(LINKSWITCH)mcodbcalc
else 
 ifeq ($(PRJ_F_DBCALC),NO)
  PRJ_LDBC :=
 else 
  $(error Variable PRJ_F_DBCALC. Invalid value)
 endif
endif

ifeq ($(PRJ_F_UDA),YES)
 PRJ_LUDA := $(LINKSWITCH)mcouda
 PRJ_MCOCOMP_F_UDA := -sa -f
else 
 ifeq ($(PRJ_F_UDA),NO)
  PRJ_LUDA :=
  PRJ_MCOCOMP_F_UDA :=
 else 
  $(error Variable PRJ_F_UDA. Invalid value)
 endif
endif

ifeq ($(PRJ_F_XML),YES)
 PRJ_MCOCOMP_F_XML := -x
else 
 ifeq ($(PRJ_F_XML),NO)
  PRJ_MCOCOMP_F_XML :=
 else 
  $(error Variable PRJ_F_XML. Invalid value)
 endif
endif

ifeq ($(PRJ_F_CPP),YES)
 PRJ_MCOCOMP_F_CPP := -hpp
else 
 ifeq ($(PRJ_F_CPP),NO)
  PRJ_MCOCOMP_F_CPP :=
 else 
  $(error Variable PRJ_F_CPP. Invalid value)
 endif
endif

ifeq ($(PRJ_F_HV),YES)
  PRJ_LHV := $(LINKSWITCH)mcohv $(LINKSWITCH)mcoews_cgi_cud $(LINKSWITCH)mcouda $(SOCKETLIB)
  PRJ_SAMPLE_REQ_NET:=NET
else 
 ifeq ($(PRJ_F_HV),SQL)
  PRJ_SAMPLE_REQ_NET:=NET
  ifeq ($(PRJ_STORAGE),PERSISTENT)
   PRJ_LHV := $(LINKSWITCH)mcohv_sql $(LINKSWITCH)mcoews_cgi_cud $(LINKSWITCH)mcouda $(LINKSWITCH)mcoapi $(LINKSWITCH)mcosql $(LINKSWITCH)mcoseridsk $(SOCKETLIB)
  else
   PRJ_LHV := $(LINKSWITCH)mcohv_sql $(LINKSWITCH)mcoews_cgi_cud $(LINKSWITCH)mcouda $(LINKSWITCH)mcoapi $(LINKSWITCH)mcosql $(LINKSWITCH)mcoseri $(SOCKETLIB)
  endif
 else
  ifeq ($(PRJ_F_HV),NO)
   PRJ_LHV :=
  else 
   $(error Variable PRJ_F_HV. Invalid value)
  endif
 endif
endif

ifeq ($(PRJ_F_CLUSTER),TCP)
 PRJ_SAMPLE_REQ_NET:=NET
 PRJ_COMMON_SRC += $(MCO_SAMPLES)/common/commoncl.c
 PRJ_COMMON_DEP += $(MCO_SAMPLES)/common/commoncl.h
 PRJ_LCLUSTER := $(LINKSWITCH)mcocluster $(LINKSWITCH)mcocltcp $(SOCKETLIB)
 ifeq ($(PRJ_STORAGE),PERSISTENT)
  PRJ_LCLUSTER += $(LINKSWITCH)mcoseridsk
 else
  PRJ_LCLUSTER += $(LINKSWITCH)mcoseri
 endif
else 
 ifeq ($(PRJ_F_CLUSTER),NO)
  PRJ_LCLUSTER :=
 else 
  $(error Variable PRJ_F_CLUSTER. Invalid value)
 endif
endif


ifeq ($(PRJ_RUNTIME),AUTO)
 PRJ_LMISC := $(LINKSWITCH)mcouwrt
else 
 ifeq ($(PRJ_RUNTIME),CRT)
  PRJ_LMISC := $(LINKSWITCH)mcouwrt
 else 
  ifeq ($(PRJ_RUNTIME),NOCRT)
   PRJ_LMISC := $(LINKSWITCH)mcounrt
  else 
   ifeq ($(PRJ_RUNTIME),NONE)
    PRJ_LMISC :=
    $(error Variable PRJ_RUNTIME. Invalid value)
   endif
  endif
 endif
endif

ifeq ($(BUILD_PLATFORM),SunOS)
ifneq ($(x64),on)
ifeq ($(findstring -DMCO_CFG_USE_DIRECT_POINTERS,$(CFLAGS)$(MCO_CFG_OVERRIDE)),-DMCO_CFG_USE_DIRECT_POINTERS)
 OPTIMIZEFLAGS:=$(subst O3,O0,$(OPTIMIZEFLAGS)) 
 OPTIMIZEFLAGS:=$(subst O2,O0,$(OPTIMIZEFLAGS))
 OPTIMIZEFLAGS:=$(subst O1,O0,$(OPTIMIZEFLAGS))
 MCO_LARGE_DATABASE_SUPPORT:=off
 MCO_CFG_OVERRIDE:=$(subst -DMCO_LARGE_DATABASE_SUPPORT,,$(MCO_CFG_OVERRIDE))
endif
endif
endif

PRJ_LINK  := $(foreach li, $(PRJ_LHV) $(PRJ_LSQL) $(PRJ_LUDA) $(PRJ_LDBC) $(PRJ_LHA) $(PRJ_LCLUSTER) $(PRJ_LLOG) $(PRJ_LSERI) $(PRJ_LVT) $(PRJ_LSYNC) $(PRJ_LMEM) $(PRJ_LFS) $(PRJ_LTMGR) $(PRJ_LMISC) $(PRJ_LRPC), $(addsuffix $(MCO_RTCHECK)$(LIBSUFFIX),$(filter-out $(SOCKETLIB),$(li))))
PRJ_LINK2 := $(foreach li, $(PRJ_LHV) $(PRJ_LSQL) $(PRJ_LUDA) $(PRJ_LDBC) $(PRJ_LHA) $(PRJ_LCLUSTER) $(PRJ_LLOG) $(PRJ_LSERI) $(PRJ_LVT) $(PRJ_LSYNC) $(PRJ_LMEM) $(PRJ_LFS) $(PRJ_LTMGR) $(PRJ_LMISC) $(PRJ_LRPC), $(addsuffix $(MCO_RTCHECK)$(LIBSUFFIX),$(li)))

ifneq ($(PRJ_LINK),$(PRJ_LINK2))
PRJ_LINK  += $(SOCKETLIB)
endif

# generated files
ifeq ($(MCO_USE_ARCH_SFX),on)
PRJ_OBJ_DIR:= obj$(MCO_ARCH_SFX)/
else
PRJ_OBJ_DIR:= obj/
endif
PRJ_DB_DIR := 
PRJ_RPC_DIR:= 

# database schemas
ifneq ($(PRJ_SCHEMAS),)
PRJ_DB_DIR   := $(PRJ_OBJ_DIR)db/
PRJ_DB_DIR_I := -I$(PRJ_DB_DIR)
PRJ_DB_DEP   := 
PRJ_DB_SRC    = $(foreach li, $(foreach db, $(PRJ_DBNAMES), $(addsuffix .c,$(db))), $(addprefix $(PRJ_DB_DIR), $(li)))
PRJ_DB_GEN    = $(PRJ_DB_SRC) $(foreach li, $(foreach db, $(PRJ_DBNAMES), $(addsuffix .h,$(db))), $(addprefix $(PRJ_DB_DIR), $(li)))
ifeq ($(PRJ_F_CPP),YES)
PRJ_DB_GEN   += $(PRJ_DB_SRC) $(foreach li, $(foreach db, $(PRJ_DBNAMES), $(addsuffix .hpp,$(db))), $(addprefix $(PRJ_DB_DIR), $(li)))
endif
endif # $(PRJ_SCHEMAS) != ""

# rpc interfaces
ifneq ($(PRJ_INTERFACES),)
PRJ_RPC_DIR   := $(PRJ_OBJ_DIR)rpc/
PRJ_RPC_DIR_I := -I$(PRJ_RPC_DIR) -I$(dir $(PRJ_INTERFACES))
PRJ_RPC_DEP   := 
PRJ_RPC_SRC    = $(foreach li, $(foreach i, $(basename $(notdir $(PRJ_INTERFACES))), $(addsuffix _dict.c,$(i)) ), $(addprefix $(PRJ_RPC_DIR), $(li)) )

ifeq ($(PRJ_RPC),CLIENT)
PRJ_RPC_SRC   += $(foreach li, $(foreach i, $(basename $(notdir $(PRJ_INTERFACES))), $(addsuffix _client.c,$(i)) ), $(addprefix $(PRJ_RPC_DIR), $(li)) )
endif

ifeq ($(PRJ_RPC),SERVER)
PRJ_RPC_SRC   += $(foreach li, $(foreach i, $(basename $(notdir $(PRJ_INTERFACES))), $(addsuffix _server.c,$(i)) ), $(addprefix $(PRJ_RPC_DIR), $(li)) )
endif

PRJ_RPC_GEN    = $(foreach li, \
                     $(foreach i, \
                         $(basename $(notdir $(PRJ_INTERFACES))), \
                         $(addsuffix _dict.c,$(i)) $(addsuffix _client.c,$(i)) $(addsuffix _server.c,$(i)) $(addsuffix _server.h,$(i)) \
                       ), $(addprefix $(PRJ_RPC_DIR), $(li)) )
endif # $(PRJ_INTERFACES) != ""

# source and object files and dependences
PRJ_SRC_C   = $(foreach li, $(filter %.c,  $(PRJ_SOURCES)), $(addprefix $(PRJ_OBJ_DIR), $(li))) $(PRJ_COMMON_SRC)
PRJ_SRC_CPP = $(foreach li, $(filter %.cpp,$(PRJ_SOURCES)), $(addprefix $(PRJ_OBJ_DIR), $(li))) 
PRJ_SRC     = $(PRJ_DB_SRC) $(PRJ_RPC_SRC) $(PRJ_SRC_C) $(PRJ_SRC_CPP)
PRJ_DEP     = $(PRJ_DB_DEP) $(PRJ_RPC_DEP) $(filter %.h,$(PRJ_SOURCES)) $(filter %.hpp,$(PRJ_SOURCES)) $(PRJ_COMMON_DEP)
PRJ_OBJ     = $(PRJ_DB_SRC:%.c=%$(OBJSUFFIX)) $(PRJ_RPC_SRC:%.c=%$(OBJSUFFIX)) $(PRJ_SRC_C:%.c=%$(OBJSUFFIX)) $(PRJ_SRC_CPP:%.cpp=%$(OBJSUFFIX))

# Build rules
MCO_INCLUDE      += -I$(MCO_SAMPLES)/common $(PRJ_DB_DIR_I) $(PRJ_RPC_DIR_I) $(MCO_SQL_INCLUDE) $(MCO_ODBC_INCLUDE)

all: $(PRJ_BIN_FILE)

# Pretty log
.PHONY: .prj-title .prj-schemas .prj-interfaces .prj-clean .prj-distclean .prj-tmp-dirs
ifndef SHOW_BUILD_LOG
MAKEFLAGS    += --no-print-directory
MSG_COMPILE   = @echo -n "Compiling $< ... "
MSG_LINK      = @echo -n "Building  $@ ... "
MSG_DONE      = @echo done
BPFX          = @
BSFX          = 1>>build-log 2>&1
.prj-start-log:
	@echo "Current directory is $(PWD)" > build-log
.prj-title:
	@echo 
	@echo    "Project      - $(PRJ_TITLE)" $(GEN_FILE)
.prj-schemas:
	@echo -n "Schema(s)    - $(PRJ_SCHEMAS), processing ... "
.prj-interfaces:
	@echo -n "Interface(s) - $(PRJ_SCHEMAS), processing ... "
.prj-clean: .prj-title
	@echo -n "Removing intermediate files ... "
.prj-distclean:
	@echo -n "Removing resulting, input and output files ... "
.prj-tmp-dirs:
	@echo -n "Creating build structure ... "
.prj-tmp-done:
	$(MSG_DONE)
else
MSG_COMPILE  :=
MSG_LINK     :=
MSG_DONE     :=
BPFX         :=
BSFX         :=
.prj-title .prj-schemas .prj-interfaces .prj-clean .prj-distclean .prj-tmp-dirs .prj-tmp-done:
endif # SHOW_BUILD_LOG

# Pretty log
ifndef SHOW_BUILD_LOG
QUIET_COMPILE  =@echo Compiling $< ...
QUIET_FINALIZE =@echo Building $(MCO_TITLE) ... ; \
cat $(MCO_BUILD_LOG_DIR)/$(MCO_TITLE)$(MCO_LOGFILE_SFX) | grep 'MCO-MESSAGE' | sed -e 's/.*MCO-MESSAGE *//'
QUIET_PREFIX   =@
#QUIET_SUFFIX   =1>>$(MCO_BUILD_LOG_DIR)/$(MCO_TITLE)$(MCO_LOGFILE_SFX) 2>>$(MCO_BUILD_LOG_DIR)/$(MCO_TITLE)$(MCO_LOGFILE_SFX)
QUIET_SUFFIX   = >>$(MCO_BUILD_LOG_DIR)/$(MCO_TITLE)$(MCO_LOGFILE_SFX) 2>&1
MAKEFLAGS     += --no-print-directory
else
QUIET_COMPILE  =
QUIET_FINALIZE =
QUIET_PREFIX   =
QUIET_SUFFIX   = 
#QUIET_SUFFIX   = 2>&1 | tee -a $(MCO_BUILD_LOG_DIR)/$(MCO_TITLE)$(MCO_LOGFILE_SFX)
endif

ifdef GENERATOR
MSG_COMPILE  :=
MSG_LINK     :=
MSG_DONE     :=
BSFX         :=
BSFX          = $(GEN_FILE)
QUIET_SUFFIX := $(GEN_FILE)
endif

$(PRJ_DB_DEP) $(PRJ_DB_SRC): .prj-schemas $(PRJ_SCHEMAS)
	$(BPFX)true $(foreach s, $(PRJ_SCHEMAS), && $(MCOCOMP) $(PRJ_MCOCOMP_F_X32_X64) -P$(PRJ_DB_DIR) $(PRJ_MCOCOMP_F_XML) $(PRJ_MCOCOMP_F_UDA) $(PRJ_MCOCOMP_F_CPP) $(PRJ_MCOCOMP_F_SQL) $(MCOCOMP_EXTRA_FLAGS) $(s) $(BSFX) )
	$(MSG_DONE)

$(PRJ_RPC_DEP) $(PRJ_RPC_SRC): .prj-interfaces $(PRJ_INTERFACES)
	$(BPFX)true $(foreach s, $(PRJ_INTERFACES), && $(MCORCOMP) -p$(PRJ_RPC_DIR) $(s) $(BSFX) )
	$(MSG_DONE)

$(PRJ_DB_DIR)%$(OBJSUFFIX) : $(PRJ_DB_DIR)%.c $(PRJ_DB_DEP)
	$(MSG_COMPILE)
	$(BPFX)$(CC) $(CFLAGS) $(MCO_CFG_OVERRIDE) $(MCO_INCLUDE) $(CCCOMPF) $< $(CCOUTPUTF) $(MSVCCCOUTPUTF)$@ $(BSFX)
	$(MSG_DONE)

$(PRJ_RPC_DIR)%$(OBJSUFFIX) : $(PRJ_RPC_DIR)%.c $(PRJ_RPC_DEP)
	$(MSG_COMPILE)
	$(BPFX)$(CC) $(CFLAGS) $(MCO_CFG_OVERRIDE) $(MCO_INCLUDE) $(CCCOMPF) $< $(CCOUTPUTF) $(MSVCCCOUTPUTF)$@ $(BSFX)
	$(MSG_DONE)

$(PRJ_OBJ_DIR)%$(OBJSUFFIX) : %.c $(PRJ_DEP) # $(PRJ_DB_DEP) $(PRJ_RPC_DEP)
	$(MSG_COMPILE)
	$(BPFX)$(CC) $(CFLAGS) $(MCO_CFG_OVERRIDE) $(MCO_INCLUDE) $(CCCOMPF) $< $(CCOUTPUTF) $(MSVCCCOUTPUTF)$@ $(BSFX)
	$(MSG_DONE)

$(PRJ_OBJ_DIR)%$(OBJSUFFIX) : %.cpp $(PRJ_DEP) # $(PRJ_DB_DEP) $(PRJ_RPC_DEP)
	$(MSG_COMPILE)
	$(BPFX)$(CXX) $(CXXFLAGS) $(MCO_CFG_OVERRIDE) $(MCO_INCLUDE) $(CXXCOMPF) $< $(CXXOUTPUTF) $(MSVCCXXOUTPUTF)$@ $(BSFX)
	$(MSG_DONE)

$(PRJ_OBJ_DIR) $(PRJ_DB_DIR) $(PRJ_RPC_DIR) : .prj-tmp-dirs
	$(BPFX)$(MKDIR) $@ $(BSFX)

PRJ_SAMPLE_REQ=$(shell grep sample_os_initialize $(PRJ_SOURCES) | sed -e's~.*(~~' -e's~).*~~' -e's~|~ ~g')

ifeq ($(PRJ_SAMPLE_REQ_FS),)
PRJ_SAMPLE_REQ_FS =$(filter DISK,$(PRJ_SAMPLE_REQ))
endif
ifeq ($(PRJ_SAMPLE_REQ_NET),)
PRJ_SAMPLE_REQ_NET=$(filter NET,$(PRJ_SAMPLE_REQ))
endif

ifeq ($(NMALLOC),on)
$(PRJ_BIN_FILE): .prj-title 
	@echo "The sample requres malloc() support"

run: 
	@echo Sample: $(PRJ_TITLE)
	@echo "The sample requres malloc() support"
else
ifeq ($(NSHAREDMEM)-$(PRJ_MEMDEV),on-SHARED)
$(PRJ_BIN_FILE): .prj-title 
	@echo "The sample requres shared memory support"

run: 
	@echo Sample: $(PRJ_TITLE)
	@echo "The sample requres shared memory support"
else
ifeq ($(PRJ_SAMPLE_REQ_FS)-$(NFS),DISK-on)
$(PRJ_BIN_FILE): .prj-title 
	@echo "The sample requres file system support"

run: 
	@echo Sample: $(PRJ_TITLE)
	@echo "The sample requres file system support"
else 
ifeq ($(PRJ_SAMPLE_REQ_NET)-$(NN),NET-on)
$(PRJ_BIN_FILE): .prj-title 
	@echo "The sample requres network support"

run: 
	@echo Sample: $(PRJ_TITLE)
	@echo "The sample requres network support"
else 
$(PRJ_BIN_FILE): .prj-title $(PRJ_OBJ_DIR) $(PRJ_DB_DIR) $(PRJ_RPC_DIR) .prj-tmp-done $(PRJ_OBJ)
ifneq ($(PRJ_SIDE_DIR),)
	$(MSG_LINK)

#ifneq ($(or $(strip $(PRJ_SRC_CPP)),$(filter $(LINKSWITCH)mcosql% $(LINKSWITCH)mcoapi% $(LINKSWITCH)mcorsql%, $(PRJ_LINK))),)
ifneq ($(strip $(PRJ_SRC_CPP)),)
	$(BPFX)$(LDCXX) $(LDCXXFLAGS) $(LIBSWITCH)$(MCO_ROOT)/$(PRJ_SIDE_DIR)/$(PRJ_BIN_DIR) $(PRJ_OBJ) $(PRJ_LINK) $(PLATFORMLIB) $(PTHREADLIB) $(LDOUTPUTF) $(MSVCLDOUTPUTF)$@$(EXESUFFIX) $(BSFX)
else
 ifneq ($(filter $(LINKSWITCH)mcosql% $(LINKSWITCH)mcoapi% $(LINKSWITCH)mcorsql%, $(PRJ_LINK)),)
	$(BPFX)$(LDCXX) $(LDCXXFLAGS) $(LIBSWITCH)$(MCO_ROOT)/$(PRJ_SIDE_DIR)/$(PRJ_BIN_DIR) $(PRJ_OBJ) $(PRJ_LINK) $(PLATFORMLIB) $(PTHREADLIB) $(LDOUTPUTF) $(MSVCLDOUTPUTF)$@$(EXESUFFIX) $(BSFX)
 else
	$(BPFX)$(LD) $(LDFLAGS) $(LIBSWITCH)$(MCO_ROOT)/$(PRJ_SIDE_DIR)/$(PRJ_BIN_DIR) $(PRJ_OBJ) $(PRJ_LINK) $(PLATFORMLIB) $(PTHREADLIB) $(LDOUTPUTF) $(MSVCLDOUTPUTF)$@$(EXESUFFIX) $(BSFX)
 endif
endif
	$(MSG_DONE)
else
	@echo "No final binary for this project"
endif

run: $(PRJ_BIN_FILE)
	@echo Sample: $(PRJ_TITLE)
ifneq ($(PRJ_SIDE_DIR),)
	$(BPFX)$(PRJ_BIN_FILE) $(BSFX)
	$(MSG_DONE)
else
	@echo "Not application. Skipped"
endif
endif # $(PRJ_SAMPLE_NET)-$(NN),NET-on
endif # $(PRJ_SAMPLE_FS)-$(NFS),DISK-on
endif # $(NSHAREDMEM)-$(PRJ_MEMDEV)
endif # NMALLOC

clean: .prj-clean
	$(BPFX)$(RMFILE) $(PRJ_OBJ) $(PRJ_DB_GEN) $(PRJ_RPC_GEN)
	$(BPFX)$(RMDIR)  $(PRJ_DB_DIR) $(PRJ_RPC_DIR) $(PRJ_OBJ_DIR)
	$(MSG_DONE)
	
distclean: clean .prj-distclean
ifneq ($(PRJ_SIDE_DIR),)
	$(BPFX)$(RMFILE) $(PRJ_BIN_FILE)
endif
	$(BPFX)$(RMFILE) vxbuild*.lst $(BSFX)
	$(MSG_DONE)
	
endif # PRJ_TYPE == APP


PLATFORMLIB       += -ltmc
